UITableViewCell dynamic height :/

独自空忆成欢 提交于 2019-11-30 04:08:05

See this tutorial, http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

The trick is to make the label grow with the size of the cell, than you can just set the size of the cell and the cell will grow with it.

Set the timeAgo label to align it self to the bottom of the cell.

Set the numberOfLines of tweet to 0 via IB,re move all the draw code and only implement the following:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item  = [self.item objectAtIndex:indexpath.row];

    CGFloat height = 85.0f;

    if ([item isKindOfClass:[Tweet class]]) {
        Tweet *tweet = (Tweet *)item;
        CGSize titleSize = [tweet.tweet sizeWithFont:[UIFont fontWithName:@"Arial" size:13.0f] constrainedToSize:CGSizeMake(260.0f, MAXFLOAT)];

        // adde the 24 pixels to get the height plus the time ago label.
        height =  titleSize.height + 24.0f;

    } else if( [item isKinfOfClass:[SC_Release class]]) {
        height = 65.0f;
    }

   return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [tweetsArray indexPath.row];
    CGSize labelSize = [string sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] 
                                                     constrainedToSize:CGSizeMake(280.0f, MAXFLOAT) 
                                                         lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 20;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!