TableView里面的UICollectionView没有填充单元格?

时间:2015-07-23 01:17:53

标签: ios swift uitableview uicollectionview

我有一个tableView,其行需要水平流动collectionViews。

我的tableView单元格中有一个collectionView,然后我将它们实例化为:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell
    let nibName = UINib(nibName: "CollectionCell", bundle: nil)
    cell.collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CollectionCell")
    cell.collectionView.dataSource = self
    cell.collectionView.delegate = self

    return cell
}

我正在尝试使用自动高度表视图行,我认为这可能会导致问题。如何将UITableViewAutomaticDimension与内部集合视图一起使用?

2 个答案:

答案 0 :(得分:7)

这是在objective-c中,但对我有用:

我。在自定义单元格的.m文件中注册UICollectionViewCell笔尖,并指定布局详细信息(大小,间距等)。

II。在包含tableView的VC中,其.m在cellForRowAtIndexPath

中执行以下操作
 MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyTableViewCellId" forIndexPath:indexPath];

    cell.collectionView.delegate = self;
    cell.collectionView.dataSource = self;
    cell.collectionView.tag = indexPath.row;

    [cell.collectionView reloadData];

III。您可以在委托方法中使用UICollectionView的标记来填充正确的信息

跳这有帮助。

答案 1 :(得分:0)

尝试添加 - cell.setTranslatesAutoresizingMaskIntoConstraints(false) - 见下文

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell
    let nibName = UINib(nibName: "CollectionCell", bundle: nil)
    cell.collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CollectionCell")
    cell.collectionView.dataSource = self
    cell.collectionView.delegate = self
    cell.setTranslatesAutoresizingMaskIntoConstraints(false)

    return cell
}

有关详情,请参阅https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells

相关问题