开发者

Custom UITableViewCell with height defined in UITableView

I have a custom UITableViewCell. In its function - (id)init开发者_开发知识库WithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier, I need to know the cell height as defined in the tableView function heightForRowAtIndexPath so I can properly position UITextField, UIButton, etc in the cell. . Any ideas?


The way I usually go about doing this is I add a method to my NSObject subclass that will act as my datasource object (what goes into the datasource array, assuming you're using this basic approach).

eg. Say we need to display a bunch of blog posts (pure text), each post is a cell. Since each row will have variable height, I create an NSObject subclass, call it BlogPostInfo. In this class, I add the method:

- (int)cellHeight;
{
    /* Perform a calculation with blog data, probably using sizeWithFont: */
}

Since you have this method in your data object, you can use it as follows in the UITableViewController:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    /* assuming blogPosts is an NSMutableArray or whatevs */
    return [[blogPosts safeObjectAtIndex:indexPath.row] cellHeight];
}

That's how I do dynamic heights of tableviewcells.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜