如何在一个视图控制器中集成多个集合视图?

5

多个集合视图

我正试图使用UICollectionViews获取七个水平滚动的按钮栏。我没有问题地启动和运行一个按钮栏,但是当我使用多个 Collection Views 时,我遇到了应用崩溃的错误。有人知道如何在 Swift 中运行此操作或者知道任何教程吗?这里是我第一个 Collection View 的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var tableImages: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png"]

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tableImages.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell:CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
        cell.expansionCell.image = UIImage(named: tableImages[indexPath.row])
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        println("cell \(indexPath.row) selected")
    }
}

Please help!


你遇到了哪些错误?在哪一行? - ABakerSmith
1
有很多教程将UICollectionView放在UITableViewCell中,因此可以创建一个包含UICollectionView的UITableView,从而在UITableView中获取多个UICollectionView。参考链接:http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/ - andykkt
我收到了线程1的信号SIGABRT。 - user4724346
你在哪一行遇到了那个错误? - ABakerSmith
从这里开始:https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html。这是一个没有错误说明的非问题。 - danh
是的,这是一个有趣的问题... 我不确定是否可能在不增加性能开销的情况下从单个集合视图中运行。 - UKDataGeek
2个回答

1
为了分离逻辑,您可以创建3个数据源对象来填充集合视图。每个数据源都可以处理其集合视图的所有单元格逻辑。这将使您的视图控制器不那么混乱,因为您只需要在viewDidLoad中分配它们即可。您需要保留对数据源对象的引用,因为collectionView.dataSource不会保留它们。
var collectionViewA: UICollectionView!
var dataSourceA: DataSourceA!
var collectionViewB: UICollectionView!
var dataSourceB: DataSourceB!
var collectionViewC: UICollectionView!
var dataSourceC: DataSourceC!

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSourceA = DataSourceA()
    self.collectionViewA.dataSource = self.dataSourceA
    // repeat for dataSource/collectionView B and C
}

1
很好的建议,但几乎肯定与OP的问题无关。 - danh
那是我的点赞,但你的评论混淆了本来不错的建议。将数据源分开实例化(甚至类)是一个好的设计,但它们的执行在主线程上是串行的。我不确定你所说的“资源崩溃”是什么意思?还有哪些问题源于代码的无数问题之一?从我看到的情况来看,OP将拥有三个呈现相同图像集合的集合。你的-仍然很好-原始建议将为OP节省他/她需要区分视图的条件逻辑。 - danh

0

这是我处理的方式,希望能对你有所帮助。

插座:

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var SecondCollectioView: UICollectionView!

方法:

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

    if(collectionView == self.SecondCollectioView) {
        cell.backgroundColor = UIColor.black
    } else {
         cell.backgroundColor = self.randomColor()
    }

    return cell;
}

如果集合视图的ReuseIdentifier不同,我们应该编写什么?请查看我的问题https://dev59.com/Kafja4cB1Zd3GeqPqhzE - Saeed Rahmatolahi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接