我有TableView with Sections,这些部分是国家,我从我的核心数据实体获取它们。我为我的tableview设置了一个背景图片,现在我想更改该部分的标题颜色。我怎样才能做到这一点?到目前为止,这是我的代码。
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
//label.text = sectionTitle;
label.text=[sectionInfo name];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width,tableView.bounds.size.height)];
[view addSubview:label];
return [sectionInfo name];
}
当我尝试我的代码时出现错误
答案 0 :(得分:1)
您只需要在该方法中返回一个字符串(标题)。退房:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
这是你应该定制外观的地方。
答案 1 :(得分:0)
如果您想更改UITableView中各个部分的颜色,您应该使用UITableViewDelegate中的此方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section