更改UITable部分backgroundColor而不丢失部分标题

bru*_*h51 7 title background-color sectionheader ios

我已经更改了tableviews部分的backgroundColor/backgroundImage.
我使用普通的tableView.不用担心,使用此代码可以正常工作:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//For using an image use this:
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

return sectionView;
}  
Run Code Online (Sandbox Code Playgroud)

现在的问题是,章节标题已经消失.

我正在使用以下代码设置该部分的标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    return @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}


}
Run Code Online (Sandbox Code Playgroud)

如果我不使用,我会得到章节标题

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
Run Code Online (Sandbox Code Playgroud)

需要你的帮助.
非常感谢.

编辑1:

感谢Mutix:

就我而言,答案是:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {




UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)];
titleLabel.backgroundColor = [UIColor clearColor];

//section Title
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    titleLabel.text = @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    titleLabel.text = [sectionInfo name];
}



[sectionView addSubview:titleLabel];
[titleLabel release];

return sectionView;
Run Code Online (Sandbox Code Playgroud)

}

Aug*_*ine 13

从iOS6开始,我们有更好的解决方案:

有一个委托方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

您可以像这样使用此方法

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

     //Set the background color of the View
     view.tintColor = [UIColor blueColor];

     // Text Color
     UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
     [header.textLabel setTextColor:[UIColor whiteColor]];

}
Run Code Online (Sandbox Code Playgroud)


Mut*_*tix 6

viewForHeaderInSection重写的titleForHeaderInSection方法.

要在使用标题时向标题添加标题,viewForHeaderInSection需要在标题标题视图中添加标签,如下所示:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];

    sectionView.backgroundColor = [UIColor greenColor];

    //Add label to view
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
    titleLabel.text = @"title";
    [sectionView addSubview:titleLabel];
    [titleLabel release];

    return sectionView;
}  
Run Code Online (Sandbox Code Playgroud)