如何在Swift的UIViewController中添加多个收藏视图?

f0brbegy  于 2022-11-21  发布在  Swift
关注(0)|答案(6)|浏览(146)

我努力了许多天才意识到这一点:

我想在我的UIViewController中添加两个不同的CollectionView。例如,我想把图像放在这些CollectionView中,每个CollectionView使用自己的图像。这可能吗?
如果有人能给予我一把,我会很高兴的

h9a6wy2h

h9a6wy2h1#

这是可能的,您只需要将每个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)
}

在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
    }
}

在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
}
omhiaaxx

omhiaaxx2#

是的--这是完全可能的。您可以将它们各自的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)    
    }
}

UICollectionView的子类如下:

class CustomCollectionViewA: UICollectionView {
    // add more subclass code as needed
}

class CustomCollectionViewB: UICollectionView {
    // add more subclass code as needed
}
dtcbnfnu

dtcbnfnu3#

您可以使用工厂设计模式来构建两个不同的集合视图,并通过函数返回它们。
此代码位于单独的帮助器文件中:

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

}

}

下面是视图控制器代码:

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
    }

}

}

Result

9rbhqvlz

9rbhqvlz4#

您还可以对集合视图outlet进行不同的命名(无需子类化):

@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var SecondCollectioView: UICollectionView!

实验方法:

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;
}

这是将另一种方式。

mnemlml8

mnemlml85#

下面是我的swift 5和Xcode 11的工作版本:
为相应的收藏视图创建插座:插座:

@IBOutlet weak var bgCollectionView: UICollectionView!
@IBOutlet weak var frontCollectionView: UICollectionView!
var arrImages = [String : [UIImage]]()

arrImages包含如下内容

override func viewDidLoad() {
        super.viewDidLoad()
    arrImages = [
    "frontImg": [//Front UIImage array],
    "bgImg": [//Background UIImage array]
    ]
}

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let arrImg = arrImages["bgImg"] {
            return arrImg.count
        } else if let arrImg = arrImages["frontImg"]{
            return arrImg.count
        }
        return 0
    }

"你有两种方法"
1.使用CollectionView插座

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell    
    if collectionView == self.bgCollectionView{
            if let arrImg = arrImages["bgImg"]{
                cell.imgView.image = arrImg[indexPath.row]
            }
        }else{
            if let arrImg = arrImages["frontImg"]{
                cell.imgView.image = arrImg[indexPath.row]
            }
        }
        return cell
    }

1.使用CollectionView标记:此处,Background Images(背景图像)集合的视图标记为1,Front Images(正面图像)集合的视图标记为2。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    if collectionView == collectionView.viewWithTag(1){
        if let arrImg = arrImages["bgImg"]{
            cell.imgView.image = arrImg[indexPath.row]
        }
    }else{
        if let arrImg = arrImages["frontImg"]{
            cell.imgView.image = arrImg[indexPath.row]
            }
        }
        return cell
    }

请在CollectionView中添加标记,如下所示:

谢谢。希望它对你有用!!

sqyvllje

sqyvllje6#

斯威夫特5回答!
如果尝试将两个collectionViews连接到同一视图控制器,Xcode将抛出错误“Outlets cannot connect to repeating content”(插座无法连接到重复内容)
解决方法:
前往情节提要
1.通过outlet连接第一个collectionView,在viewDidLoad中设置delegate/dataSource,然后通过指向故事板中的属性检查器向第二个collectionView添加标记,并将值从0更改为1
1.选择secondCollectionView并转到连接检查器,选择delegate并将连接拖动到UIViewController,对于dataSource也是如此。
1.只需检查正在通过哪个collectionView。

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

     if collectionView == collectionView.viewWithTag(1) {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCollectionView", for: indexPath)
     return cell
 }

 else {

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCollectionView", for: indexPath) as! HomeMainCollectionViewCell

     cell.configureCell()

     return cell
  }

相关问题