【发布时间】:2013-11-16 02:48:11
【问题描述】:
我知道我们可以将背景图像/颜色添加到动态表格视图/单元格中的部分标题,但任何人都可以帮助我在使用静态单元格的表格视图中做同样的事情吗?
我想要做的是在使用静态单元格的 TableView 中为我的第三部分使用背景图像(它总共有 4 个部分)
我想添加背景图像并更改文本颜色以说明第三部分的一些 RGB 值
【问题讨论】:
标签: ios iphone objective-c xcode cocoa-touch
我知道我们可以将背景图像/颜色添加到动态表格视图/单元格中的部分标题,但任何人都可以帮助我在使用静态单元格的表格视图中做同样的事情吗?
我想要做的是在使用静态单元格的 TableView 中为我的第三部分使用背景图像(它总共有 4 个部分)
我想添加背景图像并更改文本颜色以说明第三部分的一些 RGB 值
【问题讨论】:
标签: ios iphone objective-c xcode cocoa-touch
您可以使用 UITableView 的委托方法来设置节标题的高度和视图。这应该做你想做的事:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 100:30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
label.text = @"Description";
label.textColor = [UIColor whiteColor];
[imageView addSubview:label];
imageView.image = [UIImage imageNamed:@"House.tiff"];
return (section == 2)? imageView:nil;
}
【讨论】:
UITableViewCells 具有backgroundView 属性,即UIView。例如,您可以将其更改为UIImageView,或者为您的表格视图单元格构建更复杂的背景。单元格是静态的还是动态的都无关紧要。
然后可以通过设置UITableViewCells cell.textLabel.textColor 来更改单元格的文本颜色。
同样适用于UITableViewHeaderFooterView,它们(顾名思义)用于表格视图部分的页眉和页脚。在代码中,您可以使用
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
这使您无需从头开始完全重新创建标题,而只是进行小的调整。或者,覆盖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
并构建一个自定义的UIView,它将成为您的标题。
【讨论】:
你应该给静态单元格分配一个标签(比如 15)
tableview:didSelectRowForIndexPath
然后
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag==15)
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:250/255.0 blue:243/255.0 alpha:1.0];
}
【讨论】: