iOS/Swift: 运行 时 UITableViewCell 中的两个嵌入视图未出现

iOS/Swift: Two embedded views in UITableViewCell not appearing when run

我正在尝试将 UIStackView 嵌入到 UITableViewCell 中。 UIStackView 里面应该有两个项目:一个 UICollectionView 和另一个 UITableView

我采用的方法是将所有内容嵌入根 UIViewController 中包含的 UIView 中。

我从父 UITableView 创建了一个 dataSource 出口,并使根 UIViewController 符合 UITableViewDataSource。之后,我实现了标准的 UITableView 功能,并对 UITableViewCell 中的嵌入式 UICollectionView 做了类似的事情。这是根 UIViewController 和自定义 UITableViewCell:

的代码
class OverviewController: UIViewController, UITableViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "AppTitle"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "appCell", for: indexPath) as! AppCell

        return cell
    }
}

class AppCell: UITableViewCell, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "featureCell", for: indexPath) as! AppCollectionCell

        return cell
    }
}

当我 运行 这样做时,我得到一个空的 table 视图:

我做错了什么?

我为你做了一个小项目:https://github.com/mvdizel/q44833725

  1. 使用与项目中相同的数据源引用
  2. 小心实施数据源方法

您的 top table view 有数据源 - 您的视图控制器。 另一个 table 视图(在顶部 table 视图单元格内)和集合视图,都有数据源 - 您的单元格。