7

我已经更改了我的 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;
}  

现在的问题是,章节标题不见了。

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

- (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];
}


}

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

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

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

编辑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;

}

4

5 回答 5

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]];

}
于 2014-02-28T07:39:19.907 回答
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;
}  
于 2011-12-07T10:06:24.997 回答
2

从 UITableViewDelegate 实现下面的方法,以及需要的 ui 视图。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView()
    view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
    return view
}

如果您需要更改页脚,请在 ...viewForFooterInSection.. 方法中使用类似的代码

想画自己的字体,使用类似的自定义:

...    
let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
label.font = UIFont(name: "HelveticaNeue", size: 14)!
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.contentView.addSubview(label)

view.textLabel.hidden = true
...
于 2015-03-13T22:43:31.860 回答
1

只需添加[titleLabel setBackgroundColor:[UIColor clearColor]];
,因为在我的情况下,标签与绿色重叠。

于 2013-02-12T13:47:20.530 回答
0

~最佳解决方案 iPhone 3.5 和 4 屏幕~

从此代码设置背景颜色或背景图像。这里不用计算截面高度,在xib中设置即可。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)];
    sectionView.backgroundColor = [UIColor greenColor];
    
    return sectionView;
}
于 2013-09-14T07:21:09.867 回答