UITableView with various prototypes and cell heights using storyboards

I am currently working on my first iOS application and just ran into a problem that I cannot solve. After long hours of reflection and struggle, I decided to ask here to see if someone has encountered a similar problem and can help me.

I have a UITableView with two cell prototypes. I can display both types of cells without problems, but there is a small catch. One type of cell has a height of 475 pixels, and the other only 250 pixels. When a table view becomes visualized, each row of the table has the same height. I went ahead and debugged my storyboard and found that my table had a line height of 475 pixels. This is normal for a higher cell, but for small ones they leave an empty space between them.

I would like to be able to adjust the height of each row depending on what type of cell it is being rendered. Is it possible? I was thinking about using heightForRowAtIndexPath, but since there are no cells at the time of execution, I cannot verify what the cell class is at a specific index. In addition, in my storyboard, I set the height for each of the two cell prototypes, but it is not reflected at runtime. Does anyone have an idea how to solve this problem? Thank you for your promotion.

+4
source share
2 answers

You need to use heightForRowAtIndexPath, but you do not have to check the table to find out which cell the index is in. You must check your data source.

, cellForRowAtIndexPath - ...

if ( /*some condition*/ ) {
    cell = //dequeue cell type 1
} else {
    cell = //dequeue cell type 2
}

, heightForRow .

if ( /*some condition*/ ) {
    return 475;
} else {
    return 250;
}
+5

, UITableViewDelegate tableView: heightForRowAtIndexPath:

, - , , . , , .

heightForRowAtIndexPath , . .

+2

Source: https://habr.com/ru/post/1545870/


All Articles