Reputation: 41
I'm using a static Table View to show some data and since my data is dynamic, I need a Cell with can adjust its size according to its content. I have override the heightForRowAtIndexPath:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 4 {
let size = itemDescription.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
return size.height
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
I have tried both UILayoutFittingCompressedSize
and UILayoutExpandedSize
and both will return a CGSize
with 140 in height which is not enough to show my text.
I also tried to increase the number of lines in the UILabel
, but it doesn't work either.
On storyboard, my Cell is a basic TableViewCell
.
Does anyone have any idea of what's going on?
Upvotes: 1
Views: 992
Reputation: 4176
To use self-sizing cells you don't need to override heightForRowAtIndexPath
. Override estimatedHeightForRowAtIndexPath
and return either an estimate or UITableViewAutomaticDimension
. And, it's important, you have to have explicit constraints to all 4 sides of a cell. For UILabel
specify 0 as numer of lines to enable text auto-height.
Upvotes: 3