为什么标准UITableViewCell中UILabel的帧是(0,0,0,0),UITableViewCell.textLabel的标准大小是什么?

时间:2013-01-31 05:49:58

标签: objective-c xcode4.5

我想用webview替换它,这样我就可以显示html。

PO(cell.subviews);
PO(cell);
PO(cell.contentView.subviews);

webview需要与UITableViewCell框架具有相同的框架。

这就是我们得到的:

2013-01-31 12:37:35.204 BadgerNew[3888:c07] cell.subviews: (
    "<UITableViewCellContentView: 0xa057140; frame = (0 0; 320 44); layer = <CALayer: 0xa057180>>"
)
2013-01-31 12:37:35.204 BadgerNew[3888:c07] cell: <UITableViewCell: 0xa0c37a0; frame = (0 0; 320 44); text = 'atm'; layer = <CALayer: 0xa0c8530>>
2013-01-31 12:37:35.205 BadgerNew[3888:c07] cell.contentView.subviews: (
    "<UILabel: 0xa0d12a0; frame = (0 0; 0 0); text = 'atm'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0xa0571b0>>"
)

此外,进一步确认

po [cell textLabel]
(UILabel *) $1 = 0x0a0d12a0 <UILabel: 0xa0d12a0; frame = (0 0; 0 0); text = 'atm'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0xa0571b0>>

那么普通UITableViewCell的最终标准框架是什么?

我也做了

-(void)layoutSubviews
{
    PO(self);
    PO(self.textLabel);
    while (false);
}

得到了

2013-02-01 09:36:02.048 BadgerNew[1436:c07] self.textLabel: <UILabel: 0x8923390; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x89e8fc0>>

中完成
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    while (false);
}

产生相同的结果

然后我做了:

-(void)layoutSubviews
{
    [super layoutSubviews];
    PO(self);
    PO(self.textLabel);
    while (false);
}

再次,另一个0帧。

1 个答案:

答案 0 :(得分:2)

单元格内子视图的帧在-tableView:cellForRowAtIndexPath之后设置。

创建UITableViewCell的子类,它确实是处理这类事情的最佳方式。您可以覆盖子类中的-textLabel并将其弯曲到您的意愿,或者您可以覆盖-layoutSubviews并使用-textLabel的框架。

最后一个警告是,在对象内部造成破坏会导致代码崩溃,这可能会破坏iOS的任何版本。