gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:22:57 25 4
gpt4 key购买 nike

我想要一个 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")
}
}

最佳答案

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

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

}

关于ios - 如何在一个 View Controller 中拥有多个 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com