简体   繁体   English

如何在一个View Controller中拥有多个Collection View?

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

I want one View Controller to have seven horizontal button scrolls. 我希望一个View Controller具有七个水平按钮滚动。 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. 我已经设置了第一个“集合视图”,并且直接在其下面重复了第二个“集合视图”的确切过程,但是我一直收到线程1:SIGABRT错误。 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. 因此,当我在下面添加第二个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")
}
}

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

}

暂无
暂无

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

相关问题 如何在一个视图控制器中合并多个集合视图? - 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? 一个视图控制器上的3个集合视图 - 3 Collection Views on One View Controller 我如何快速在集合视图中加载多个自定义视图 - How can i load multiple custom views in collection view swift 我可以使用一个视图控制器在选项卡栏控制器中处理多个视图吗 - Can I use one view controller to handle multiple views in a tab bar controller 一个具有多个视图的视图控制器 - one view controller with multiple views 在具有多个集合视图的视图 Controller 中的特定集合视图上应用 UICollectionViewDelegateFlowLayout - Apply UICollectionViewDelegateFlowLayout on specific Collection View in View Controller with multiple Collection Views 如何让集合视图中的图像视图根据屏幕大小自行调整大小? - How can I have image views in a collection view resize themselves depending on the screen size? 如何快速在一个视图控制器中将占位符设置为多个文本视图 - How to set placeholder to multiple text views in one view controller in swift 一个视图控制器可以处理多少个子视图和表视图 - How many subviews and table views can one view controller handle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM