Setting variable UITableViewCell height

后端 未结 2 1984
面向向阳花
面向向阳花 2021-01-14 20:10

I am using a custom UITableViewCell, which has some labels. There is one label in the cell where the string length is variable, due to this i cannot set a constant height to

2条回答
  •  鱼传尺愫
    2021-01-14 21:07

    -heightForRowAtIndexPath: is where you get to set it. I know putting that logic in the tableview delegate is sort of ugly, but it can't be done in the cell.

    Think about it, the tableview has to know the height of the cell before it even instantiates the cell so it can calculate the length of the scroll bar (which is proportional). That means you cannot calculate the size of the cell in the cell itself because the size of the cell has to be known well in advance of the cell ever being instantiated.

    In other words, you can't set the row height in the cell because the tableview needs to know the height of cells it will never instantiate if the user doesn't scroll to them.

提交回复
热议问题