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

用户名

我希望一个View Controller具有七个水平按钮滚动。我已经设置了第一个“集合视图”,并且直接在其下面重复了第二个“集合视图”的确切过程,但是我不断遇到线程1:SIGABRT错误。我的功能集合视图代码在这里:

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

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

如何在一个Laravel View表单中扩展两个部分?

来自分类Dev

一个View Controller上有多个NSTableView

来自分类Dev

如何在WatchOS 2和Swift中将变量从一个View Controller传递到另一个View Controller

来自分类Dev

如何在Mobile View中获得一个按钮来填充页脚的整个宽度?

来自分类Dev

如何在DotnetNuke中重定向到另一个View(UserControl'.ascx')?

来自分类Dev

如何从另一个View Controller访问Tab View Controller

来自分类Dev

如何在Android的另一个类中调用带有View类型参数的函数

来自分类Dev

如何在SwiftUI中创建一个带有View并返回自定义结果的函数?

来自分类Dev

多个DropDownLists和一个View and View模型

来自分类Dev

在android Translate-Animation中如何在另一个view2上方翻译view1,两个视图均以不同的布局退出

来自分类Dev

在另一个View Controller中停止线程

来自分类Dev

如何从另一个View Controller中的变量获取值

来自分类Dev

如何在IBM Worklight中重定向到同一页面但又重定向到另一个div / view?

来自分类Dev

如何将多个 View 的 DataContext 设置为一个 ViewModel 实例

来自分类Dev

从另一个View Controller使用原型单元

来自分类Dev

验证后移至下一个View Controller

来自分类Dev

即使一个API失败也要加载View Controller

来自分类Dev

删除viewcontroller并移至下一个view controller

来自分类Dev

Catel:多个 ViewModel 一个 View。是否可以?

来自分类Dev

从另一个View Controller Swift 2.0更改View Controller图像

来自分类Dev

如何获取ViewPager的当前View,这是onPageSelected中向用户显示的一个视图

来自分类Dev

如何从Microsoft SQL 2008中的每个VIEW中选择一个元素

来自分类Dev

如何从另一个类的 Recycler View 中删除项目?

来自分类Dev

如何在android的布局的多个组件中拥有一个唯一的按钮?

来自分类Dev

如何在android的布局的多个组件中拥有一个唯一的按钮?

来自分类Dev

如何通过动画UIView将UIImage设置为SWIFT中的另一个View Controller

来自分类Dev

一个View Controller中有两个TableView

来自分类Dev

从一个View Controller导航到另一个

Related 相关文章

  1. 1

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

  2. 2

    如何在一个Laravel View表单中扩展两个部分?

  3. 3

    一个View Controller上有多个NSTableView

  4. 4

    如何在WatchOS 2和Swift中将变量从一个View Controller传递到另一个View Controller

  5. 5

    如何在Mobile View中获得一个按钮来填充页脚的整个宽度?

  6. 6

    如何在DotnetNuke中重定向到另一个View(UserControl'.ascx')?

  7. 7

    如何从另一个View Controller访问Tab View Controller

  8. 8

    如何在Android的另一个类中调用带有View类型参数的函数

  9. 9

    如何在SwiftUI中创建一个带有View并返回自定义结果的函数?

  10. 10

    多个DropDownLists和一个View and View模型

  11. 11

    在android Translate-Animation中如何在另一个view2上方翻译view1,两个视图均以不同的布局退出

  12. 12

    在另一个View Controller中停止线程

  13. 13

    如何从另一个View Controller中的变量获取值

  14. 14

    如何在IBM Worklight中重定向到同一页面但又重定向到另一个div / view?

  15. 15

    如何将多个 View 的 DataContext 设置为一个 ViewModel 实例

  16. 16

    从另一个View Controller使用原型单元

  17. 17

    验证后移至下一个View Controller

  18. 18

    即使一个API失败也要加载View Controller

  19. 19

    删除viewcontroller并移至下一个view controller

  20. 20

    Catel:多个 ViewModel 一个 View。是否可以?

  21. 21

    从另一个View Controller Swift 2.0更改View Controller图像

  22. 22

    如何获取ViewPager的当前View,这是onPageSelected中向用户显示的一个视图

  23. 23

    如何从Microsoft SQL 2008中的每个VIEW中选择一个元素

  24. 24

    如何从另一个类的 Recycler View 中删除项目?

  25. 25

    如何在android的布局的多个组件中拥有一个唯一的按钮?

  26. 26

    如何在android的布局的多个组件中拥有一个唯一的按钮?

  27. 27

    如何通过动画UIView将UIImage设置为SWIFT中的另一个View Controller

  28. 28

    一个View Controller中有两个TableView

  29. 29

    从一个View Controller导航到另一个

热门标签

归档