Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the cell object in tableView:(UITableView *)tableView heightForRowAtIndexPath function?

I wand to know the content of the cell when returning the heightForRowAtIndexPath. In this function how do I get the cell object?

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

}

If I try this

 [tableView cellForRowAtIndexPath:indexPath]

it fails and goes off in an infinite loop.

Any idea?

like image 509
user348398 Avatar asked Apr 12 '11 17:04

user348398


People also ask

How do you make a tableView cell Unselectable?

To completely prevent selection of the UITableViewCell , have your UITableViewDelegate implement tableView:willSelectRowAtIndexPath: . From that method you can return nil if you do not want the row to be selected. This prevents the row from being selected and tableView:didSelectRowAtIndexPath: from being called.


3 Answers

Call self (the delegate) rather than the tableView itself.

id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
like image 53
NWCoder Avatar answered Sep 28 '22 22:09

NWCoder


Use: func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject? of UITableView.
Don't use: func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
Possible Reason: heightForRowAtIndexPath gets called before cellForRowAtIndexPath and this method uses the index path to perform additional configuration based on the cell’s position in the table view. But since, there is still no cell yet, hence it goes to infinite loop I think.

like image 23
Puneet Sharma Avatar answered Oct 01 '22 22:10

Puneet Sharma


The cell hasn't been created when heightForRowAtIndexPath: is called, so there's no way to "get" a cell. Instead, look at your model and determine the height of the cell that way.

If you're concerned about long strings and needing more vertical space in your cell, consider using sizeWithFont:forWidth:lineBreakMode:. Docs here: https://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

like image 24
innesngunn Avatar answered Sep 28 '22 22:09

innesngunn