如何在iPhone iOS4中将UITableViewCellStyleSubtitle textAlignment设置为居中?

时间:2010-06-23 09:31:58

标签: iphone uitableview ios4

自从使用iPhone SDK 4将XCode升级到版本3.2.3后,我的代码不再起作用了。

我有一个样式为UITableViewCellStyleSubtitle的默认单元格,并希望将textAlignment textLabeldetailTextLabel设置为center,但没有任何反应。 之前使用的相同代码现在不再起作用了。在UITableViewCellStyleDefault中心对齐仍然有效。

有谁知道如何解决这个问题?我实际上并不想使用自定义单元格。

3 个答案:

答案 0 :(得分:4)

文本不居中的原因是标签只与文本一样宽。您可以通过设置文本标签的背景颜色来确认:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textLabel.backgroundColor = [UIColor orangeColor];
}

手动设置单元格宽度似乎也没有效果。因此,您应该添加自己的子视图或创建自己的UITableViewCell子类。

UITableViewCellStyleSubtitle的文档甚至说:

  

具有左对齐的单元格的样式   标签顶部和一个   它下面的左对齐标签更小   灰色文字。 iPod应用程序使用   这种风格的细胞。

答案 1 :(得分:1)

这是一个由两部分组成的解决方案,用于在渲染后更改标签宽度,从而实现居中:

-- (void) updateCenteringForTextLabelInCell:(UITableViewCell*)cell {

    UILabel* label = cell.textLabel;

    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, cell.contentView.frame.size.width, label.frame.size.height);

}


-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

// do your usual

// call the helper method 

[self performSelector:@selector(updateCenteringForTextLabelInCell:) withObject:cell afterDelay:0.05];

}

答案 2 :(得分:0)

好的,最后Apple开发团队在2010年6月23日回答了我的错误报告:

  

“请尝试设置文字对齐方式   在调用后的layoutSubviews中   超级“。