`

iphone开发--UITableView中的cell高度不一致解决方案

阅读更多

事先声明:本例没有做任何性能上的考虑, 而且写的很生硬,只是演示思路。如果是有大批量的cell,比如10000个cell,需要使用缓存记录之前的cell的高度以优化。

 

主要代码如下

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YYSHDomain *domain = [self.array objectAtIndex:indexPath.row];
    return [self cellHeight:domain];
}

// 获取cell的高度
- (CGFloat) cellHeight:(YYSHDomain *)domain {
    UILabel *_titleLable = [[UILabel alloc] initWithFrame:CGRectMake(5, 30, 320 - 10, 0)];
    [_titleLable setNumberOfLines:0];
    [_titleLable setText:domain.title];
    NSLog(@"%@", NSStringFromCGRect(_titleLable.frame));
    [_titleLable sizeToFit];
    CGRect frame = _titleLable.frame;
    [_titleLable release];
    NSLog(@"%@", NSStringFromCGRect(frame));
    if (domain.imgName) {
        return frame.origin.y + frame.size.height + 5 + 60 + 5;
    } else {
        return frame.origin.y + frame.size.height + 5;
    }
}
 

代码见附件

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics