简体   繁体   English

一个视图控制器上的3个集合视图

[英]3 Collection Views on One View Controller

I have 3 collection views on 1 view controller. 我在1个视图控制器上有3个集合视图。 I've tried a few of the other suggestions I've found on Stack but nothing seems to work. 我尝试了一些在Stack上发现的其他建议,但似乎没有任何效果。

All 3 Collection Views are in a separate cell of a HomeTableViewController. 所有3个集合视图都位于HomeTableViewController的单独单元中。 I tried to create the outlet connection to the HomeTableViewController but I get the error Outlets cannot be connected to repeating content . 我尝试创建到HomeTableViewController的插座连接,但出现错误: Outlets cannot be connected to repeating content

I've read many people being able to hook up their multiple collectionViews so I am a bit confused as to where I'm going wrong... 我已经读过很多人能够连接他们的多个collectionViews,所以对于我要去哪里出错,我有点困惑...

The UICollectionView instances cannot be hooked up to IBOutlet properties in a separate UITableViewController . 无法将UICollectionView实例连接到单独的UITableViewController IBOutlet属性。

As you describe, the UICollectionView s are actually each children of their own parent UITableViewCell , and as such are not direct descendants of the UITableViewController . 正如您所描述的, UICollectionView实际上是它们各自父级UITableViewCell每个子级,因此不是UITableViewController直接后代。 This is because the cells will be added to the UITableView at run time. 这是因为单元格将在运行时添加到UITableView中。

If you are set on creating the outlets within the HomeTableViewController I would suggest creating them like so: 如果您HomeTableViewControllerHomeTableViewController创建插座,则建议像这样创建它们:

private weak var collectionViewA: UICollectionView?

and overriding cellForRow like so: 并像这样覆盖cellForRow

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)
    // cast cell as collection view parent
    collectionViewA = cell.collectionView
    return cell
}

As has been mentioned, the better solution would be to manage the UICollectionView instances from within their own UITableViewCell parents. 如前所述,更好的解决方案是从自己的UITableViewCell父级内部管理UICollectionView实例。 Example: 例:

final class CollectionViewAParentTableViewCell: UITableViewCell {
    @IBOutlet private weak var collectionView: UICollectionView!
}
extension CollectionViewAParentTableViewCell: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        …
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        …
    }
}

You should create outlet in the UITableViewCell. 您应该在UITableViewCell中创建插座。 Then you can provide tags to collectionViews in each cell in tableView:cellForRowAtIndexPath Method: 然后,您可以在tableView:cellForRowAtIndexPath方法的每个单元格中为collectionViews提供标签:

yourCell.collectionViewOutlet.tag = indexPath.row + 1000

you should replace 1000 with Any Constant Integer if the tags conflict with tags of other views. 如果标记与其他视图的标记冲突,则应使用Any Constant Integer替换1000。

Then use these tags to differentiate all collectionviews in collectionview:cellForItemForIndexpath method: 然后使用这些标记来区分collectionview:cellForItemForIndexpath方法中的所有collectionview:

if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}

You should also keep in mind to return number of items for each collection view just like above. 您还应该记住,像上面一样,为每个集合视图返回项目数。

Tags make the life whole lot easier , don't they? 标签使生活变得更加轻松,不是吗? Happy Coding (Y) 快乐编码(是)

You should create outlet in the UITableViewCell. 您应该在UITableViewCell中创建插座。 Then you can provide tags to collectionViews in each cell in tableView:cellForRowAtIndexPath Method: 然后,您可以在tableView:cellForRowAtIndexPath方法的每个单元格中为collectionViews提供标签:

yourCell.collectionViewOutlet.tag = indexPath.row + 1000

you should replace 1000 with Any Constant Integer if the tags conflict with tags of other views. 如果标记与其他视图的标记冲突,则应使用Any Constant Integer替换1000。

Then use these tags to differentiate all collectionviews in collectionview:cellForItemForIndexpath method: 然后使用这些标记来区分collectionview:cellForItemForIndexpath方法中的所有collectionview:

if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}

You should also keep in mind to return number of items in collectionView:numberOfItemsInSection for each collection view just like above. 您还应该记住,为每个集合视图返回collectionView:numberOfItemsInSection中的项目数,就像上面一样。

Tags make the life whole lot easier , don't they? 标签使生活变得更加轻松,不是吗? Happy Coding (Y) 快乐编码(是)

Create three different collection view with different collection view cell, then after you just need to add in dataSource method like below:- 使用不同的集合视图单元格创建三个不同的集合视图,然后只需添加如下所示的dataSource方法即可:

if collectionView == collectionViewA{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellA", for: indexPath) as! collectionCell

        return cell
    }else if collectionView == collectionViewB{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellB", for: indexPath) as! collectionCell

        return cell
    }else if collectionView == collectionViewC{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellC", for: indexPath) as! collectionCell

        return cell
    }else{
        return UICOllectionViewCell()
}

also perform same for other dataSource method. 对其他dataSource方法也执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个视图控制器中具有不同重用单元格的两个集合视图 - Two Collection Views with Different Reuse Cells in One View Controller 如何在一个View Controller中拥有多个Collection View? - How can I have multiple Collection Views in one View Controller? 如何在一个视图控制器中合并多个集合视图? - How to incorporate multiple collection views in one view controller? 如何有一个视图 controller 有两个集合视图,但只有一个有页眉/页脚视图? - How to have a view controller with two collection views, but only one has header/footer views? 一个具有多个视图的视图控制器 - one view controller with multiple views 在集合视图控制器中快速移动两个视图 - Swift a two views within a collection view controller 在具有多个集合视图的视图 Controller 中的特定集合视图上应用 UICollectionViewDelegateFlowLayout - Apply UICollectionViewDelegateFlowLayout on specific Collection View in View Controller with multiple Collection Views 为什么 didSelect function 仅适用于视图 controller 中的两个集合视图之一? - Why didSelect function only works for one of two collection views in a view controller? 如何在一个视图中制作两个集合视图? - How to make two collection views in one view? Xcode IOS One View Controller在2个视图上 - Xcode IOS One View Controller on 2 views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM