首先需要在Storyboard中创建好TableViewController,使用动态Cell,在Prototype Cells中设计好Cell界面。
接着,定义好Autolayout,注意Autolayout一定要在上下都绑定控件的位置,不要只从上到下定义,只有正确定义Autolayout,后面我们用到的systemLayoutSizeFittingSize方法才会返回正确的结果。
如下图:
Xcode会提示Autolayout的各种Ambiguity,提示修改控件的Compression resistance,如下图:
这里让Xcode智能修正就可以了,具体哪个控件的Compression resistance无所谓,因为我们最终的目的是让UITabelViewCell的高度去适合所有控件的大小。
然后,因为是在Xcode 5 iOS 7模式下设计的Storyboard,所以在iOS 7下运行肯定是没问题的:
接着在iOS 6上运行:
出现这个问题的原因是:iOS 7和iOS 6中的许多控件默认高度都是不一样的,在其他普通UIView下,有了Autolayout,控件当然会正确显示。但是UITableViewCell的高度是通过UITableView的heightForRowAtIndexPath方法来返回的。默认情况下,它是保持不变的。所以当Cell内控件的高度发生变化后,如果Cell高度没有因此而作出调整,肯定会出问题的。
那么怎样解决问题呢?理想状态下是这样的,在UITableView的cellForRowAtIndexPath方法中创建并返回Cell,然后在heightForRowAtIndexPath方法中计算并返回Cell的高度。
但是真正的执行顺序是相反的,如果在TableView中有10个Row(假设都可以显示在屏幕上的话,这样不存在Cell的重用),iOS会先调用10次heightForRowAtIndexPath,然后再调用10次cellForRowAtIndexPath。也就是说按照iOS的执行顺序,我们要在Cell创建前知道他的高度。
那么是不是可以在heightForRowAtIndexPath中先创建Cell,并返回高度,然后在之后的cellForRowAtIndexPath调用接着使用这个Cell?好主意!不过问题远没有没有想象中简单。如果在heightForRowAtIndexPath调用dequeueReusableCellWithIdentifier:forIndexPath:方法的话,会出现栈溢出问题,类似这样:
也就是说dequeueReusableCellWithIdentifier:forIndexPath:会反过来调用heightForRowAtIndexPath方法。
还没完,还有一个问题,多次调用dequeueReusableCellWithIdentifier:forIndexPath:方法会产生不同的Cell,即便是IndexPath是一样的,可以做个很简单的示例证明:
//在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法内
static NSString *CellIdentifier = @"MyCell";
MyCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
MyCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(@"%d", cell1 == cell2);
结果会输出0。cell1不会等于cell2的。
哈哈,冷静。问题很快会解决的,第一个问题,heightForRowAtIndexPath无法使用dequeueReusableCellWithIdentifier:forIndexPath:方法,那么我们可以使用旧的dequeueReusableCellWithIdentifier方法,也就是没有IndexPath参数的,这个是可以使用的,当然使用dequeueReusableCellWithIdentifier的话,我们需要手动判断Cell返回nil的情况。
第二个问题,我们不去YY着dequeueReusableCellWithIdentifier:forIndexPath:会按照IndexPath来返回Cell,自己在heightForRowAtIndexPath方法中提前缓存创建的Cell,Key就是IndexPath,Value是Cell,然后在cellForRowAtIndexPath方法中使用缓存的Cell就OK。
那么,在TableViewController中加入必要的字段:
//测试数据源
NSMutableArray *_dataSource;
//缓存Cell
NSMutableDictionary *_cellCache;
在viewDidLoad中初始化相关数据:
//viewDidLoad 初始化
_dataSource = [NSMutableArray arrayWithArray:@[@"Mgen", @"Tony", @"Jerry", @"一二三"]];
_cellCache = [NSMutableDictionary dictionary];
把创建Cell逻辑写在一个方法内(注意在heightForRowAtIndexPath:indexPath中无法使用dequeueReusableCellWithIdentifier:forIndexPath:方法,所以这里需使用dequeueReusableCellWithIdentifier方法):
- (MyCell*)getCellFromIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"MyCell";
//注意在heightForRowAtIndexPath:indexPath无法使用dequeueReusableCellWithIdentifier:forIndexPath:
MyCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//用dequeueReusableCellWithIdentifier:就得判断Cell为nil的情况
if (!cell)
{
cell = [[MyCell alloc] init];
}
//这里把数据设置给Cell
cell.titleLabel.text = [_dataSource objectAtIndex:indexPath.row];
return cell;
}
在UITableView中执行画龙点睛一笔,使用systemLayoutSizeFittingSize方法来计算创建Cell的高度并返回,如下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取Cell
MyCell *cell = [self getCellFromIndexPath:indexPath];
//缓存Cell
[_cellCache setObject:cell forKey:@(indexPath.row)];
//更新UIView的layout过程和Autolayout
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
//通过systemLayoutSizeFittingSize返回最低高度
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
接着在cellForRowAtIndexPath方法内重用缓存的Cell(代码里还有如果没有缓存再次调用创建Cell的逻辑,不过目前觉得没这种可能性,因为heightForRowAtIndexPath方法发生在cellForRowAtIndexPath方法之前):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取缓存的Cell
MyCell *cachedCell = [_cellCache objectForKey:@(indexPath.row)];
//如果没有缓存再次调用getCellFromIndexPath来创建Cell
if (!cachedCell)
{
return [self getCellFromIndexPath:indexPath];
}
return cachedCell;
}
OK,现在Cell无敌了。即便是你把Cell高度手动调整成这样:
在iOS 6下会显示出正确的结果:
iOS 7下也下一样:
源代码下载
下载页面
注意:链接是微软SkyDrive页面,下载时请用浏览器直接下载,用某些下载工具可能无法下载
源代码环境:Xcode 5.0