开发者

iPhone - Custom TableViewCell height calculated off cell text

I'm creating a custom cell in the cellForRowAtIndex function and filling it with my dynamic text. The height of the cell is variable based upon this text so I need to user the heightForRowAt function, but in that function I don't have the cell. 开发者_StackOverflow中文版I can create a new one and fill it with the text and then call my calculateHeight function on it but then I'm creating 2 cells for each row.

Anyone have a better method? Cache the cells yourself in the cellForRow function in a dictionary by row index and use that? Is cellForRow called before heightForRow?

I've managed to crash Xcode4 twice so far playing around with this so am hoping someone has done this already.


tableView:heightForRowAtIndexPath:

is called before

tableView:cellForRowAtIndexPath:

Creating two cells is a possible solution, but creating two cells isn't a very effective method. However, you don't need the cell to calculate the height; All you need is the string.

You'll probably want to use

sizeWithFont:constrainedToSize:

on your string to calculate the height.

I could provide details on how to do that, but you should be able to figure it out from reading the documentation.


What you do is you make a class method on your custom cell (for instance), which knows the values of how things (statically sized things) will be set, and does some math to determine given the offsets of the text view for instance, it can tell you how big the cell needs to be after calculating how high your string will be given a font.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜