Set a custom Section Header in a TableView on iPhone


/ Published in: iPhone
Save to your folder(s)

This sinpet is extracted from a post of "Undefined Value", Kris Johnson's Blog.


Copy this code and paste it in your HTML
  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  2. NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
  3. if (sectionTitle == nil) {
  4. return nil;
  5. }
  6.  
  7. // Create label with section title
  8. UILabel *label = [[[UILabel alloc] init] autorelease];
  9. label.frame = CGRectMake(20, 6, 300, 30);
  10. label.backgroundColor = [UIColor clearColor];
  11. label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
  12. saturation:1.0
  13. brightness:0.60
  14. alpha:1.0];
  15. label.shadowColor = [UIColor whiteColor];
  16. label.shadowOffset = CGSizeMake(0.0, 1.0);
  17. label.font = [UIFont boldSystemFontOfSize:16];
  18. label.text = sectionTitle;
  19.  
  20. // Create header view and add label as a subview
  21. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
  22. [view autorelease];
  23. [view addSubview:label];
  24.  
  25. return view;
  26. }

URL: http://undefinedvalue.com/2009/08/25/changing-background-color-and-section-header-text-color-grouped-style-uitableview

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.