简体   繁体   中英

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

I want one View Controller to have seven horizontal button scrolls. I've set up the first Collection View, and I repeated the exact process for a second Collection View directly underneath, but I keep getting Thread 1: SIGABRT error. My code for the functional Collection View is here:

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")
}   

}

It works when I run this.

So when I add a second Collection View underneath, create a new View Controller file to IBOutlet the button's image and a new Collection View Cell file to add the following code, it crashes.

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")
}
}

Here is a two-section version of the code you wrote. I'm not sure if it addresses the error you are seeing, but it's how you'd do multiple collection views on one page.

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")
    }   

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM