Reputation: 10245
I have added this code to my UITableViewController to return grey header sections.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
return headerView;
}
however in doing this now I cannot see the header characters... do I need to add this as a subview? or is there another way of doing things?
I have these methods adding the headers and the header text to the UITablView but when I use the above method I cannot see them anymore.
// Section headers
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionLetterArray objectAtIndex:section];
}
// Section header titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sectionLetterArray;
}
Upvotes: 1
Views: 6371
Reputation: 438467
If you create your own tableView:viewForHeaderInSection
, you have to create the UILabel or whatever, and fill in the text yourself, e.g.:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSectionTitleLeftMargin,
kSectionTitleTopMargin,
tableView.bounds.size.width - kSectionTitleLeftMargin - kSectionTitleRightMargin,
30.0 - kSectionTitleTopMargin - kSectionTitleBottomMargin)];
// do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
Clearly, change headerLabel to be set up anyway you want, but the key take-home message is that if you create your own view, you have to create the label and fill in the text yourself.
Upvotes: 7
Reputation: 19922
When you implement that method, the default view, complete with the label, is replaced by what YOU return, so it's your task to add it a label with whatever text you want.
Upvotes: 1