自从使用 iPhone SDK 4 将 XCode 升级到版本 3.2.3 后,我的代码不再工作。
我有一个样式为 UITableViewCellStyleSubtitle
的默认单元格,并且希望将 textLabel
和 detailTextLabel
的 textAlignment
设置为center
,但什么也没发生。
之前使用的相同代码现在不再工作。在 UITableViewCellStyleDefault
上居中对齐仍然有效。
有谁知道怎么解决这个问题吗?事实上我不想仅使用自定义单元格。
最佳答案
文本不居中的原因是标签仅与文本一样宽。您可以通过设置文本标签的背景颜色来确认这一点:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.backgroundColor = [UIColor orangeColor];
}
手动设置单元格宽度似乎也没有效果。因此,您确实应该添加自己的 subview 或创建自己的 UITableViewCell
子类。
UITableViewCellStyleSubtitle
的文档甚至说:
A style for a cell with a left-aligned label across the top and a left-aligned label below it in smaller gray text. The iPod application uses cells in this style.
关于iphone - 如何在 iPhone iOS 4 中设置 UITableViewCell 样式副标题文本对齐方式居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100447/