事先声明:本例没有做任何性能上的考虑, 而且写的很生硬,只是演示思路。如果是有大批量的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; }}?
代码见附件