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:
But after I click this cell and select it, the title did resize:
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?
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.
viewDidLayoutSubviews()Called to notify the view controller that its view has just laid out its subviews.
A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With