开发者

How does the height of a UITableViewCell get set

I know that when UIKit renders a cell, it uses tableVie开发者_如何学Pythonw:heightForRowAtIndexPath: to calculate the height. My question is, how and when does that get set on the actual UITableViewCell. I want to build dynamic cells and will need to calculate the placement of text within the cell. I believe I can just use self.bounds and self.frame - I was just curious about at what point those are set - even with the use of dequeueReusableCellWithIdentifier.

Thanks.


After you return the cell from tableView:cellForRowAtIndexPath:, the tableview sets a number of properties, including the bounds of the cell, then calls tableView:willDisplayCell:forRowAtIndexPath:. The only modifications after that call are setting alpha/frame, and even then only when animating the row.

Given that, if you're using a stock UITableViewCell, you can layout your text in tableView:willDisplayCell:forRowAtIndexPath: and be assured that the cell already has the correct width/height. That said, if you're doing anything more than just the basics, you may want to consider subclassing UITableViewCell. In your subclass, you can lay out your fields in layoutSubviews and be assured that your cell will always be laid out correctly.


You can use the heightForRowAtIndexPath delegate, like this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat result;
    result = 120.0f;
    return result;
}

The above example will set any cell hat is created in the canEditRowAtIndexPath delegate.

You can alternatively use section and row values to determine which cell / sections the height is set.


You can extend custom cell

- (void)drawRect:(CGRect)rect
{
//You need to do your calculation for 
[super drawRect:rect];
}

You need to do your calculation to make it appear in the proper location of cell in drawRect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜