Tuesday, May 15, 2012

iPhone | Change Section header in UITableView

Go for it!
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section {

if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return 22;
}
// If no section header title & header needed
return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];

if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 320, 18);
label.backgroundColor = ANY_COLOR_OR_IMAGE_PATTERN;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:14];
label.text = sectionTitle;

// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];

return view;
}

Test with iOS 5 as well.. please!
Let me know if any issue.. thanks!

No comments:

Post a Comment