开发者

How to set UITableViewCellStyleSubtitle textAlignment to center in iPhone iOS4?

Since up开发者_JAVA百科graded 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.


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.


here is a two-part solution to alter the label width after rendering, thus allowing the centering to happen:

-- (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];

}


ok finally the Apple development team answered my bug report from 23rd June 2010:

"Please try setting the text alignment in layoutSubviews after calling super."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜