阅读和检查了长时间的代码后,我很自豪地拥有了一个自定义的带有节和节标题的表格视图,所有内容都来自核心数据对象。现在我需要自定义节标题和背景颜色。我看到过在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;
}
}
view
是一个UITableViewHeaderFooterView
,所以在将来的iOS版本中可能会崩溃。为了更安全,建议使用条件转换。 - Aaron Brager