UITableViewCell动态高度:/

Rol*_*ers 2 iphone dynamic row-height uitableview

嘻嘻,

我创建了一个带有NIB文件的UITableViewCell.其中有一个标签,其中包含一条推文.所以它需要是一个动态的高度.还有一个timeAgo标签必须适合推文标签下方.

我正在尝试使用frame en sizes的东西,但我无法获得完美的解决方案..我在drawrect方法的UITableViewCell文件中这样做.

self.tweet.lineBreakMode = UILineBreakModeWordWrap;
self.tweet.numberOfLines = 0;
self.tweet.font = [UIFont fontWithName:@"Arial" size:13.0f];
[self.tweet sizeToFit];  

CGFloat tweetHeight = self.tweet.frame.size.height;

self.timeAgo.lineBreakMode = UILineBreakModeWordWrap;
self.timeAgo.numberOfLines = 0;
self.timeAgo.font = [UIFont fontWithName:@"Arial" size:11.0f];
[self.timeAgo sizeToFit];

CGFloat timeAgoHeight = self.timeAgo.frame.size.height;

self.timeAgo.frame = CGRectMake(88, tweetHeight, 100, timeAgoHeight + 10.0f);
Run Code Online (Sandbox Code Playgroud)

我也尝试过在教程中找到的字符串.

的:

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {
Run Code Online (Sandbox Code Playgroud)

我的HeightForRow方法也已经不同了,因为我使用不同的单元格样式.目前,我为每个单元格样式返回一个硬值,但也需要更改为单元格高度.

希望你们能指出我的方向.

谢谢,

  • 罗尔夫

rck*_*nes 7

请参阅本教程,http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

诀窍是使标签随着细胞的大小而增长,而不是只设置细胞的大小,细胞就会随之增长.

设置timeAgo标签以将其自身对齐到单元格的底部.

通过IB将tweet的numberOfLines设置为0,重新移动所有绘制代码​​并仅实现以下内容:

- (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;
}
Run Code Online (Sandbox Code Playgroud)