how to obtain the UITableViewCell within heightForRowAtIndexPath?

后端 未结 6 660
孤城傲影
孤城傲影 2020-12-24 12:19

How does one obtain the UITableViewCell when within the heightForRowAtIndexPath method, i.e. given the indexPath?

(then I could access the

6条回答
  •  有刺的猬
    2020-12-24 13:14

    The obvious answer is to call cellForRowAtIndexPath, but you may have already discovered there are some issues with that. See this question if you haven't already: UITableView flexible/dynamic heightForRowAtIndexPath

    For my last project I used a custom subclass of UICell and implemented a method like this. I then called this from table:heightForRowAtIndexPath: (after looking up the content for that row).

    + (CGFloat) heightOfContent: (NSString *)content
    {
        CGFloat contentHeight = 
               [content sizeWithFont: DefaultContentLabelFont
                   constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
                       lineBreakMode: UILineBreakModeWordWrap].height;
        return contentHeight + DefaultCellPaddingHeight;
    }
    

提交回复
热议问题