首页 > 如何在UITableviewCell中创建UITableView(和UICollectionView)?

如何在UITableviewCell中创建UITableView(和UICollectionView)?

请允许我用Storyboard来表明我的需求。
如图,在这个tableview里面有两个大的cell,一个是另外tableview,一个是collectionview。而且这两个大的cell的高度不是固定的,当点击加载更多的时候,对应的cell里面的tableview长度会增长,cell也会增长(换言之,在这两个cell里面所有内容都需要被全部展示,而不是滑动)。请问如果是这种需求的话,该如何设计呢?


建议不要在 cell 里套 tableView

在 cell 里再加 UITableView,理论上或许可行(让外层 cell 作为内层 tableView 的 delegate),但我从没见过这样的写法,也感觉这样很不好。原因有2:

  1. delegate 复杂,view 的层叠关系复杂
  2. 这样反而不能达到你的需求。你说想要:

对应的cell里面的tableview长度会增长,cell也会增长(换言之,在这两个cell里面所有内容都需要被全部展示,而不是滑动)

但 UITableView 的高度是固定的,里面是 ScrollView。它天然是适合滚动的,而不是全部显示。

可能的代替做法

我觉得这个需求可以考虑能否把这上下两个大 cell 考虑为两个 section?上面的大 cell 改成第一个 section,里面的 cell 里左边有图片、右边两个 label 的,footer 或最后一个 cell 是“加载更多”;下面的大 cell 改成第二个 section,里面根据你 collectionView 的情况而定。比如是本来是一行排5个,可以是每个 cell 里排5个;也可以是全排在一个 view 里,把这个 view 添加到唯一的 cell 上或添加到 headerView 上。然后 footer 或最后一个 cell 是加载更多。


解决方案

你的情况其实仅仅一个collectionView就可以搞定了,根据数据源分组对应分几个section,展示更多时候,就修改对应section中的数据源,刷UI就好了,你所“必须”要关注的是你的CollectionView的contentSize以及如何Layout,还有优化的话考虑Cache layout类中的attributes.

嵌套方案

今天刚刚这么写了collectionView嵌套collectionView(被要求的,不是没事找事),说下思路(TableView嵌套做法类似,XIB/SB与纯代码区别请忽略...)
PS: 有写错的地方,希望- -CollectionView大神来喷..Orz
1)自定义cell
需要挂collectionView的cell里面贴如下代码:

//.h
@class WBrowserWhisperFlowLayout;
static NSString *BrowserWhisperCoverFlowCellIdentifier = @"BrowserWhisperCoverFlowCellIdentifier";

@interface WBrowserWhisperCoverFlowCell : UICollectionViewCell
@property (nonatomic, strong) WBrowserWhisperFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;

- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate;

// .m
- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        _layout = [[WBrowserWhisperFlowLayout alloc] init];
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _layout.itemSize = CGSizeMake(CGRectGetWidth(self.frame) - 100, 200);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        [_collectionView registerClass:[WBrowserWhisperCoverFlowItemCell class] forCellWithReuseIdentifier:BrowserWhisperCoverFlowItemCellIdentifier];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
}

- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate {
    _collectionView.dataSource = dataSourceDelegate;
    _collectionView.delegate = dataSourceDelegate;
    [_collectionView reloadData];
}

2)记住在cell中的CollectionView是在cell之后初始化的,也就是在初始化这个cell的之后要立即设置这个cell中CollectionView的数据源跟代理,不然Crash没商量。

3.1)修改cell中CollectionView的数据源(若有变化,比如数据主动/被动修改),然后修改该CollectionView属性(layoutAttribute会被修改)以及重新layout两个CollectionView。(tableView则是reloat对应的indexPath)

4)继续吐槽:CollectionView的精髓在于会不会写layout。。CollectionView让TableView可以say goodbye了,但是需要花一定的时间去学习否则很难用好,推荐资源:


请使用这个方法。

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
              withRowAnimation:(UITableViewRowAnimation)animation

如果你要在uitablewview里加一个uitableview的话,可以上网找找有没有开源的。我记得我是看到过的。
如果你在uitableview里加一个uicollection的话。我提供一个想法。因为之前的项目里遇到过一个类似的问题。说一下我的思路。

因为uitableview是支持定义cell的,可以在cell里写一个collectionview,然后在定义你的collectioncell。绝对是可行的,而且个人感觉代码量不是很多。加载数据的时候每次向cell里传递数据,然后collectionview会加载这些数据。
你可以通过代码来在cell里计算高度,这个是可以实现的。

因为电脑不在身边...就不贴代码了,如果需要的话可以给我留言我周一贴一段我写的uitableview+collection搭配的代码

【热门文章】
【热门文章】