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

后端 未结 5 1644
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 19:19

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:(UITa         


        
相关标签:
5条回答
  • 2020-12-18 19:57

    I don't think you can or you should. You'll need to determine from indexPath which data you will be displaying in the corresponding cell and thus calculate the needed height of the cell.

    So you'll need to write some code to get from indexPath to the right data in your data model.

    0 讨论(0)
  • 2020-12-18 19:59

    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

    0 讨论(0)
  • 2020-12-18 20:10

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

    id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    0 讨论(0)
  • 2020-12-18 20:13
    uitableviewcell *ce=[self.view viewwithtag:indexPath.row]; 
    

    it returns the cell object.

    0 讨论(0)
  • 2020-12-18 20:16

    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.

    0 讨论(0)
提交回复
热议问题