Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 908
HanXu Avatar asked Nov 07 '13 11:11

HanXu


People also ask

When should I use layoutSubviews?

layoutSubviews() The system calls this method whenever it needs to recalculate the frames of views, so you should override it when you want to set frames and specify positioning and sizing. However, you should never call this explicitly when your view hierarchy requires a layout refresh.

What is viewDidLayoutSubviews?

viewDidLayoutSubviews()Called to notify the view controller that its view has just laid out its subviews.

What is Uitableview in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

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

like image 105
Mirko Catalano Avatar answered Oct 17 '22 16:10

Mirko Catalano