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

How can I have multiple Collection Views in one View Controller?

我想要一个视图控制器有七个水平按钮滚动条。我已经设置了第一个 Collection View,并在正下方为第二个 Collection View 重复了确切的过程,但我不断收到 Thread 1: SIGABRT 错误。我的功能性 Collection View 代码在这里:

class xyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

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

override func viewDidLoad() {
    // Initialize the collection views, set the desired frames
}   
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return tableImages.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell:xyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! xyCollectionViewCell

    cell.xyImgCell.image = UIImage(named: tableImages[indexPath.row])

    return cell
}

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

}

当我运行这个时它起作用了。

因此,当我在下方添加第二个 Collection View 时,创建一个新的 View Controller 文件到 IBOutlet 按钮的图像和一个新的 Collection View Cell 文件以添加以下代码,它崩溃了。

class bwViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var bwTableImages: [String] = ["a", "PlasmaBlast.png", "b.png", "c.png", "d.png", "e.png", "f.png", "g.png", "h.png", "i.png", "j.png"]

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell:bwCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! bwCollectionViewCell

cell.bwImgCell.image = UIImage(named: bwTableImages[indexPath.row])

return cell
}

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

这是您编写的代码的两部分版本。我不确定它是否解决了您看到的错误,但这是您在一个页面上执行多个集合视图的方式。

class xyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var tableImagesOne: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png"]
    var tableImagesTwo: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png"]

    override func viewDidLoad() {
        // Initialize the collection views, set the desired frames
    }


    func numberOfSectionsInCollectionView(_ collectionView: UICollectionView) -> Int {
        return 2
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return tableImagesOne.count
        }
        else {
            return tableImagesTwo.count
        }
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let row = indexPath.row
        let section = indexPath.section

        let cell:xyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! xyCollectionViewCell

        if section == 0 {
            cell.xyImgCell.image = UIImage(named: tableImagesOne[row])
        }
        else if section == 1 {
            cell.xyImgCell.image = UIImage(named: tableImagesTwo[row])
        }
        return cell
    }

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

}