How to get table target cell inside tableView:heightForRowAtIndexPath:?
I am trying to get variable-height table cells implemented and what I thought would be an elegant solution turned out into a recursion/stack overflow:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *cell = (MyCustomCell *)[aTableView cellForRowAtIndexPath:indexPath];
return [cell heightForObject:...]; // passing dat开发者_高级运维a object on indexPath
}
All table cell labels and other view properties are initialized at creation time (in a usual tableView:cellForRowAtIndexPath:
way). Thus my idea was that I could move height specific computing out of table controller to my custom cell implementation like this. I could use the same cell in other table controllers without little if any code duplication. heightForObject:
could look at it's subviews, what fonts they are using and build that height value from that. Heck, I could even change font sizes for certain cell labels and they should theoretically update their height properly.
So as mentioned already - I am hitting that stack overflow issue with this code. How would I go of getting that target cell for which tableView:heightForRowAtIndexPath:
is being called?
So you want to get the selected cell?
Then write:
UITableViewCell *actCell = [self.tableView cellForRowAtIndexPath:
[self.tableView indexPathForSelectedRow]];
精彩评论