UITableViewCell标题的背景颜色

时间:2011-08-30 23:21:22

标签: ios uitableview uilabel

我想要backgroundColor的{​​{1}}部分标题视图透明。我不想格式化标题中的文本,因为我喜欢默认格式。我可以这样做:

UITableView

无需格式化-(UIView*) tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section 中的文字?我尝试过的所有内容都会覆盖标题标题中的文字(我从UILabel获得)并且我不知道如何自己格式化文本。

2 个答案:

答案 0 :(得分:2)

好吧,表格单元格没有标题。

UITableView的节标题是一个独立的视图。

是的,

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

是要实现的表委托方法。它返回一个显示表头的UIView(衍生物)。

确保您实施

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

并返回每个标题的适当高度。 (可能只是一个恒定值)

答案 1 :(得分:0)

您可以使用现有的默认标头UITableViewHeaderFooterView并更改其值。这样您就不必自己创建TextLabel,仍然可以使用tableView:titleForHeaderInSection:

请务必首先注册reuseIdentifyer:

[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];

示例:

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

    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    header.contentView.backgroundColor = [UIColor redColor];
    header.textLabel.textColor = [UIColor whiteColor];
    return header;
}