当前位置: 代码迷 >> Iphone >> iphone开发-UITableView中的cell高度不一致解决方案
  详细解决方案

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

热度:103   发布时间:2016-04-25 06:40:54.0
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;    }}
?

代码见附件

  相关解决方案