Self-sizing Table View Cells

Can you please share some tutorial for resizing a cell when image is fetched from a URL and the size of image must vary with the dimension as fetched from the URL. Initially we can display a placeholder image of a fixed height say equal to half the screen height.
Stuck with this since a long time.

Did like the lesson, Why the I have not displayed the data transmission in the second TableViewCell .Error “Values 0” work. Wha is the error? ios - Value in JSON does not display in tableviewcell custom - Stack Overflow

Great one! very helpful and practical. I will apply some of them to my work.

Hi everyone, how can I use self-sizing and fixed size cells in the same tableview?

Great tutorial!

Can create a tutorial also for having a UITableView inside a UITableViewCell. and still apply self-sizing?

Hey, I think it is due to the setting of workImageView. In the attribute inspector of the workImageView setting, you set the View → Content Mode to Aspect Fit, It will fit the image in the imageView with its height/ width radio. Sorry I can not explain obvious the difference between Scale To Fit, Aspect Fit and Aspect Fill, you can try them, and I believe you can tell the difference obviously. So that means the workImageView is indeed positioned where your constraints defined, but just its image not fill in all the scale. Hope you can understand what I means.

Hey, I have a question.
It seems that I do not need to write the code below in viewDidLoad that I can realize the cell height dynamicly.

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 300

I am using iOS 10.3.3 in Xcode 9 beta 4.

Hi @fschnek! You can do that pretty easily… Start by removing the following lines:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140

You can then implement the following UITableViewDelegate functions:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if shouldUseSelfSizingCells(for: indexPath) {
        return UITableViewAutomaticDimension // will ask for the estimatedHeightForRowAt
    } else {
        return 44 // just fix the cell to 44pt
    }
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if shouldUseSelfSizingCells(for: indexPath) {
        return 44
    } else {
        return 0 // this will never be called
    }
}

func shouldUseSelfSizingCells(for indexPath: IndexPath) -> Bool {
    // Some real check here based on the IndexPath
    return (indexPath.row % 2) == 0
}
1 Like

This was awesome! Thank you! :slight_smile:
To smooth out the cell expansion animation I had to increase the vertical compression resistance of the image view to 751 rather than decrease to 749. The artwork image then stayed a fixed size while the cell expanded to accommodate the larger text field. Otherwise, the animation contracted then re-expanded the image.

This is awesome! Thank you:slightly_smiling_face:

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]