iOSiOSios开发

UITableViewCell嵌套UITableView最佳实践

2016-07-01  本文已影响7198人  ifelseboyxx

最近在画一个界面,走了很多的弯路,总结下。
话不多说,我们先来看下效果:

nest.gif
咋看下,也就是一个有多个sectiontableView,so easy!
我们先来看下数据结构:
结构层级.png
首先,最外层是一个数组,数组里面是个对象,这个对象里面又包含一个数组,这个数组里面放的也是个对象,然后这个对象里面又包含两个数组,绕的不行....

粗略的画了下结构,大概这样:


结构图.png

下面我们来构建下数据模型:

Model

我们来分析下:

或许我们可以自定义个存放多个图片或者文本的控件,但是这样会非常麻烦,journeyrange、picurls这两个数组里面的元素是不固定的,计算位置,控制显示隐藏等非常难搞。

那还有没有别的办法呢?

思路

如果我们在 cell 里面再放一个 tableView,用来接收 journeyrange、picurls这两个数组,也就是 子tableView创建journeyrange、picurls两个section,每个section里面创建journeyrange数组个数的rowpicurls数组个数的row

实践
传递数据模型到最外层 cell 里面

- (UITableViewCell *)tableView:(UITableView *)tableView    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JANestCell *cell = [tableView dequeueReusableCellWithIdentifier:JANestCellIdentifier forIndexPath:indexPath];
    cell.dayModel = self.dataArr[indexPath.section].dayjourney[indexPath.row];
    return cell;
}

cell里面接收并重写set方法

接收数据模型
重写set
- (void)setDayModel:(dayjourney *)dayModel
{
_dayModel = dayModel;
  self.dotView.hidden = !dayModel.lastRow;
  self.lblTheme.text = dayModel.theme;
  self.lblContent.text = dayModel.content;
  self.icon.image = [UIImage imageNamed:[self fetchNameWithIconType:dayModel.type]];
  [self.nestTableView reloadData];
  self.tableHeightConstraint.constant = self.nestTableView.contentSize.height;
}

重写的set方法无非就是一些常规的赋值,不过里面有两点注意点:
1.[self.nestTableView reloadData] 记得刷新cell里面的tableView
2.self.tableHeightConstraint.constant = self.nestTableView.contentSize.height;
这句代码的作用是让nestTableView自适应cell高度,这里的tableHeightConstraint 约束,是nestTableView的一条固定高度约束 记得禁掉nestTableViewscrollEnabled属性

我们来看看xib

xib
这里有两个个注意点:
1.因为外层的 cell 里面的内容是自适应的,所以我们在给约束的时候一定要确定好垂直方向的约束,不然没法算出正确的cell高度。
2.nestTableView高度约束,如果priority直接给1000,可能会约束冲突,我们可以设置成 750

效果层级图

1.绿色大框的是最外层tableView的一个section,红底色的是tableView自定义的UITableViewHeaderFooterView
2.黄色框的是tableView一个section中的一个row
3.蓝色框的是cell里面嵌套的一个nest tableView,里面用来存放一个字符串数组以及图片数组

层次结构图

遗留问题

注意好上面的几点,其余的就是正常 tableView 的 代理以及数据源操作了,就不细说了。当弄好其余的操作,运行起来会发现:

在不同尺寸屏幕下,cell 的行高,计算的有误差!!!

计算cell行高方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JADataModel *sectionModel = self.dataArr[indexPath.section];
    self.nestCell.dayModel = sectionModel.dayjourney[indexPath.row];
    return [self.nestCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
}

上面代码中的 self.nestCell 是个专门为计算高度而生的 template layout cell

- (JANestCell *)nestCell
{
    if (!_nestCell) {
         _nestCell = [[NSBundle mainBundle] loadNibNamed:JANestCellIdentifier owner:self options:nil].firstObject;
    }
    return _nestCell;
}

问题的原因是:

没有设置对 UILable 正确的 preferredMaxLayoutWidth值,只要用到了 UILable,就一定要设置好正确的 preferredMaxLayoutWidth !!!

self.lblTheme.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 83.0f;
self.lblContent.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 83.0f;

这里的83.0f 是怎么来的呢?

计算 preferredMaxLayoutWidth

虽然设置好正确的preferredMaxLayoutWidth,就可以解决 cell 高度计算不准确的问题,但是这种设置方式很投机!
如果一行有多个宽度不确定的UILable或者水平方向上的某个控件宽度不确定,那就没法通过上面的投机方式算出正确的preferredMaxLayoutWidth
其实,preferredMaxLayoutWidth值就是 UILable 矩形框的宽度,要解决这个问题,就需要在自定义cell里面拿到UILable正确的frame,我用xib画 cell 比较多,虽然在 - (void)layoutSubviews 里面能拿到正确的UILableframe,但是此时在设置preferredMaxLayoutWidth却是没效果的

如果有谁发现解决的方法请一定告诉我,我的邮箱 jasonlieio@163.com,最后附上完整JATableCellNestTableDemo

上一篇 下一篇

猜你喜欢

热点阅读