Cannot update UI object's frame in UITableViewCell

后端 未结 6 1981
眼角桃花
眼角桃花 2021-01-19 02:18

I have a subclassed UITableViewCell.

I need to dynamically change the frame of a UILabel.

Here\'s what I\'ve done:

- (UITableViewCell *)table         


        
6条回答
  •  旧巷少年郎
    2021-01-19 02:42

    Did you consider overriding the - (void) layoutSubviews method in your custom cell (i.e. MessageCell) and layout your stuff in there? I think this is the safest place to put layout changing code in, because in other places it may not have any effect. Don't forget to call [super layoutSubviews]; in your implementation of layoutSubviews.

    Consider going with this approach and let us know if it worked out for you! But you need to consider performance implications with this approach, because layoutSubviews will be called every time the table scrolls.

    EDIT:

    to accommodate for the right frame size for your label inside layoutSubviews consider using - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode for the frame calculation.

提交回复
热议问题