UIViewController中的多个collectionView - IOS swift

Mas*_*ego 55 collectionview ios swift

我试了很多天才意识到这一点: 在此输入图像描述

我想在我的UIViewController中添加两个不同的CollectionView.例如,我想将图像放在这些collectionView中.每个CollectionView都使用自己的图像.这可能吗?

如果有人能帮助我,我会很高兴的.:)

Sam*_*Sam 119

这是可能的,您只需要将每个UICollectionView添加为子视图,并将delegate和dataSource设置为您的UIViewController.

这是一个简单的例子.假设您有一个UICollectionView正常工作,您应该能够将此代码调整为您自己的用途,以便相当容易地添加第二个:

let collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"

override func viewDidLoad() {
    // Initialize the collection views, set the desired frames
    collectionViewA.delegate = self
    collectionViewB.delegate = self

    collectionViewA.dataSource = self
    collectionViewB.dataSource = self

    self.view.addSubview(collectionViewA)
    self.view.addSubview(collectionViewB)
}
Run Code Online (Sandbox Code Playgroud)

在cellForItemAtIndexPath委托函数中:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if collectionView == self.collectionViewA {
        let cellA = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier) as UICollectionViewCell

        // Set up cell
        return cellA
    }

    else {
        let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier) as UICollectionViewCell

        // ...Set up cell

        return cellB
    }
}
Run Code Online (Sandbox Code Playgroud)

在numberOfItemsInSection函数中:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.collectionViewA {
        return 0 // Replace with count of your data for collectionViewA
    }

    return 0 // Replace with count of your data for collectionViewB
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为这"如果collectionView == self.collectionViewA"这太重了比较(你可能会在其他代码中继承这个习惯,这很糟糕).我建议使用tag属性.这基本上就是它的目的 (3认同)
  • 它不适合我!我收到此错误:***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'UICollectionView必须使用非零布局参数初始化' (2认同)

kel*_*ket 9

是的 - 这完全有可能.您可以将各自的UICollectionViewDelegates/UICollectionViewDataSources分配给不同的类,也可以将CollectionViews子类化,将委托和数据源分配给当前的viewController,并在委托方法中向下转发对collectionView的引用,如下所示:

@IBOutlet collectionViewA: CustomCollectionViewA!
@IBOutlet collectionViewB: CustomCollectionViewB!


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

    if let a = collectionView as? CustomCollectionViewA {
        return a.dequeueReusableCellWithIdentifier("reuseIdentifierA", forIndexPath: indexPath)
    } else {
        return collectionView.dequeueReusableCellWithIdentifier("reuseIdentifierB", forIndexPath: indexPath)    
    }
}
Run Code Online (Sandbox Code Playgroud)


GvS*_*rma 7

为相应的collectionviews创建出口:outlet:

@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var SecondCollectioView: UICollectionView!
Run Code Online (Sandbox Code Playgroud)

方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as UICollectionViewCell

    if(collectionView == self.SecondCollectioView) {
        cell.backgroundColor = UIColor.black
    } else {
         cell.backgroundColor = self.randomColor()
    }

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这将是另一种方式.


bro*_*tea 7

您可以使用工厂设计模式来构建两个不同的集合视图并通过函数返回它们。这是我的 swift 4 工作版本。

此代码位于单独的帮助文件中:

import UIKit

class collectionViews {

static func collectionViewOne() -> UICollectionView {

    let layout = UICollectionViewFlowLayout()
    let collectionViewOne = UICollectionView(frame: CGRect(x: 0, y: 20, width: 200, height: 100), collectionViewLayout: layout)
    return collectionViewOne

}

static func collectionViewTwo() -> UICollectionView {

    let layout = UICollectionViewFlowLayout()
    let collectionViewTwo = UICollectionView(frame: CGRect(x: 0, y: 300, width: 200, height: 100), collectionViewLayout: layout)
    return collectionViewTwo

}


}
Run Code Online (Sandbox Code Playgroud)

这是视图控制器代码:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


let collectionViewOne = collectionViews.collectionViewOne()
let collectionViewTwo = collectionViews.collectionViewTwo()

var myArray = ["1", "2"]
var myArray2 = ["3", "4"]

override func viewDidLoad() {
    super.viewDidLoad()


    collectionViewOne.delegate = self
    collectionViewOne.dataSource = self
    collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
    view.addSubview(collectionViewOne)


    collectionViewTwo.delegate = self
    collectionViewTwo.dataSource = self
    collectionViewTwo.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell2")
    view.addSubview(collectionViewTwo)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if collectionView == self.collectionViewOne {
        return myArray.count
    } else {
        return myArray2.count
    }

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == self.collectionViewOne {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)

        myCell.backgroundColor = UIColor.red

        return myCell

    } else {

        let myCell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell2", for: indexPath as IndexPath)

        myCell2.backgroundColor = UIColor.blue

        return myCell2
    }

}


}
Run Code Online (Sandbox Code Playgroud)

结果