---
title: "iphone开发--UITableView中的cell高度不一致解决方案"
author: "lpluck08"
date: "2012-05-26T22:09:27Z"
tags: []
categories: ["移动开发"]
slug: "iphone-uitableview-cell"
draft: false
lastmod: "2012-05-26T22:11:14Z"
id: 1541943
url: "https://apluck.iteye.com/blog/1541943"
---

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

 

代码见附件