2011-08-30 122 views
0

我想使UITableView的部分标题视图backgroundColor透明。我不想格式化标题中的文本,因为我喜欢默认的格式。我能做到这一点有:UITableViewCell标题的背景颜色

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

,而不必在UILabel文字的格式?我尝试过的所有内容都会覆盖部分标题中的文本(我从tableView:titleForHeaderInSection:获得),但我不知道如何自己格式化文本。

+2

你为什么怕格式化UILabel中的文本? –

+0

只是不想花费时间:-)我正在寻找默认的文本颜色,阴影,缩进,字体大小等。只是保持默认值就好了。 – SundayMonday

回答

2

那么,表格单元格没有标题。

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

是的,

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

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

请确保您实现

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

以及并返回适当的高度,每一个标题。 (可能只是一个常数值)

+1

更正为'UITableView'的部分标题8-) – SundayMonday

+1

那么,现在你已经改写了这个问题,这对我来说更加清楚。 不,viewForHeaderInSection不会更改默认外观。该方法提供了一个完整视图,用于替换该部分标题的默认外观。 我害怕如果你喜欢默认设计(奇怪虽然:),那么你将不得不将自己的设计与默认的设计。 AFAIK viewForHeaderInSection是将任何内容更改为节标题的唯一方法 –

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