views:

571

answers:

1

Since upgraded XCode to Version 3.2.3 with iPhone SDK 4 my code doesn't work anymore.

I have a default cell with style UITableViewCellStyleSubtitle and want to set the textAlignment of textLabel and detailTextLabel to center, but nothing happens. Same code used before now not working anymore. On UITableViewCellStyleDefault center alignment still works.

Does anyone know how to solve this? I don't want to use a custom cell only in fact of this.

+2  A: 

The reason why text isn't centered is because the label is only as wide as the text. You can confirm this by setting the background color of the text label:

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

Setting the cell width manually also doesn't seem to have an effect. So you should really add your own subviews or create your own subclass of UITableViewCell.

The docs for UITableViewCellStyleSubtitle even say:

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.

Lextar
you're right, but why this worked well in previous iOS 3.0? the documentation wasn't changed since then. I think the textAlignment property is normally not aligning the text rather the label.
iPortable
I just tried to run the code that colors the background in different versions of the simulator: it only works in 3.0. From 3.1 on the label is only as wide as the text. I assume they changed it in 3.1 to make drawing faster (because of the smaller views). The documentation says that the labels are left-aligned so this is ok. You can't rely on undocumented features.
Lextar