HanXu HanXu - 11 months ago 62
iOS Question

layoutSubviews in UIViewTableCell does not work

I have a class PostListTableCell, inheriting UITableViewCell, which is used as my custom cell in a UITableView.

I need to resize some labels in the cell, so I called following method in

- (void)layoutSubviews {
[super layoutSubviews];
[_titleLabel sizeToFit];

But the problem is, when the UITableView loaded, the _titleLabel does not resize:

enter image description here

But after I click this cell and select it, the title did resize:

enter image description here

Following is the code to load data:

- (PostListTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PostListTableCellIdentifier = @"PostListTableCell";
PostListTableCell *cell = (PostListTableCell*) [tableView dequeueReusableCellWithIdentifier:PostListTableCellIdentifier];

if (cell == nil) {
cell = [[PostListTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PostListTableCellIdentifier];

Post *post = (Post*)_posts[indexPath.row];
[cell loadPost:post];

return cell;

Could anyone help?

Answer Source

The problem is the autolayout that resize again the label for you with the constraints that you had set when you make the label in Interface Build, unfortunately in under autolayout the the hierarchy is Constraints > Frame