TableView 中的 UICollectionView 不填充单元格?

UICollectionView inside TableView not populating cells?

我有一个 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
}

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

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

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

二。在包含 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];

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

希望这有帮助。

尝试添加 - 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