经过漫长的阅读和检查代码,我很自豪拥有一个自定义表视图,其中包含部分和部分标题,全部由核心数据对象填充.现在我需要自定义部分标题和背景颜色.我已经看到它完成,但在一个viewForHeaderInSection方法.我的titleForHeaderInSection方法是不可能的吗?
这里有我的方法:
这里有我的方法:
-(Nsstring*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
Nsstring *sectionname = [theSection name];
if ([sectionname isEqualToString:@"text 1"]){
return @"Today";
}
else if ([sectionname isEqualToString:@"text 2"]){
return @"Tomorrow";
}
if ([[self.fetchedResultsController sections]count]>0){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
}
else{
return nil;
}
}
解决方法
以下是使用现有代码设置标题文本的示例,但可以使用UITableViewheaderfooterView调整外观:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static Nsstring *header = @"customHeader";
UITableViewheaderfooterView *vHeader;
vHeader = [tableView dequeueReusableheaderfooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewheaderfooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
}
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return vHeader;
}
如果需要,您甚至可以将UITableViewheaderfooterView子类化,就像您将UITableViewCell子类化以进一步自定义外观.