In the tableView:heightForRowAtIndexPath: method I need to get the length of cell.textLabel.text in tableView:cellForRowAtIndexPath. How can this be done?
views:
57answers:
1
+1
A:
Calling cellForRowAtIndexPath involves calling heightForRowAtIndexPath, so if you call it from heightForRowAtIndexPath, you end up in an infinite recursion.
What you can do is use your data source to examine the same data you would use to populate the cell in cellForRowAtIndexPath, and then use get the length of the text you're going to insert into cell.textLabel.text (or use NSString UIKit additions to compute what the size of your label will be).
Robot K
2010-10-11 02:26:58
But what does one do when the height of the row depends on the "available" space in the cell (that is: the width of the cell, less the margins, less the accessory view, orientation of the device, etc.) Yeah, OK, the margins are pretty static, but (just about) everything else depends on the configuration of the cell itself, which is not really available to you in heightForRow, as you've noted the infinite recursion problem. I am actively seeking a solution to this as we speak...
randallmeadows
2010-10-11 21:17:19