【问题标题】:Collection View inside Table View Cell表视图单元格内的集合视图
【发布时间】:2016-05-15 19:02:24
【问题描述】:

我想在表格视图单元格内创建集合视图,以便水平滚动元素。但是我有问题如何正确填写我的收藏视图。我需要一种将集合视图与我的数据连接起来的方法。

如何获取并循环遍历表格视图单元格以找到所需的集合视图并返回包含数据的单元格?

【问题讨论】:

  • 良好的形象 - 这是可能的,但很复杂。我强烈建议为每个UICollectionView 创建一个新的delegate / data source 模型对象,并将这些对象存储在Array 中,然后在每个UITableView 单元格中引用这些对象。您需要在运行时将每个 UICollectionView 连接到正确的模型对象。这是可行的,但有很多活动部件,因此您的设计需要经过深思熟虑。
  • @kye 谢谢)我已经向 ashfurrow 询问了我的问题,但它没有帮助我:((
  • 您可以在视图中添加collectionview,然后在tableview单元格中添加该视图,这样您可以轻松运行循环,并且collectionview和tableviewdelegate不会发生冲突。

标签: ios swift tableview tvos collectionview


【解决方案1】:

以下是如何将数据传递到表格视图单元格内的集合视图的一般示例。这个link 也是一个关于这个主题的 youtube 教程。

型号:

class ListOfParents: NSObject {
var parents:[Parent]?
}

class Parent: NSObject {
var children: [Child]?

static func fetchParents(_ completionHandler: @escaping (ListOfParents) -> ()) {
    //fetch parents data
 }

}

class Child: NSObject {

}

TableView 单元格:

class CustomTableViewController: UITableViewController {

var listOfParents: ListOfParents?

override func viewDidLoad() {
    super.viewDidLoad()
    Parent.fetchparents { (listOfParents) in
        self.listOfParents = listOfParents
    }
    tableView.register(CustomParentCell.self, forCellReuseIdentifier: "tableCell")

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard let parentsCount = listOfParents?.parents?.count else {return 0}
    return parentsCount
}

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

    cell.parent = listOfParents?.parents?[indexPath.item]
    return cell
 }
}

父单元格:

class CustomParentCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {


var parent: Parent? {
    didSet {
        // set child value here
    }
}

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    return collectionView
}()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(CustomChildCell.self, forCellWithReuseIdentifier: "childCellID")
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    guard let childrenCount = parent?.children?.count else {return 0}
    return childrenCount
}

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

    cell.child = parent?.children?[indexPath.item]
    return cell
 }
}

子单元:

class CustomChildCell: UICollectionViewCell {
var child: Child?
}

【讨论】: