gpt4 book ai didi

ios - swift 。如何在 ViewController 以外的单独的 swift 文件中编写 UICollectionView

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

我找到了一堆关于如何使用 UICollectionView 的教程。但他们只是将它们写在 ViewController 中。我想知道如何在另一个 swift 文件中编写 UICollectionView。然后在我的 ViewController 类中调用它。这是我的 UICollectionView 代码。

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

var collectionView: UICollectionView!
var ImageNames: [String] = ["photo", "photo1", "photo2", "photo3"]

override func viewDidLoad() {
super.viewDidLoad()

let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ImageNames.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let imageView = UIImageView(image: UIImage(named: ImageNames[indexPath.row]))
cell.backgroundView = imageView
return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.backgroundView = nil
}

}

对了,第二个问题。在我的代码中,我目前通过使数组成为全局数组来将数组传递给 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 函数。如何在本地将 ImagesNames 数组传递给此函数?

最佳答案

重要的是

collectionView.dataSource = self
collectionView.delegate = self

它告诉您的 Collection View 在哪里可以找到它的数据源/委托(delegate)方法,例如 cellForItemAtIndexPath

你应该创建一个包含 UICollectionViewDataSourceUICollectionViewDelegate 的新类,并在你的 View Controller 中创建一个惰性变量

新类(class)...

class MyDataController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {

// all collection view stuff here

}

然后在你的 ViewController 中

lazy var dataController: MyDataController = {

let dataController = MyDataController()

return dataController
}()

最后

collectionView.dataSource = self
collectionView.delegate = self

成为

collectionView.dataSource = self.dataController
collectionView.delegate = self.dataController

关于你的第二个问题,你需要仔细考虑如何使用 MVC 约定来存储你的数据。您可以在 dataController 中有一个本地数组变量来控制 collectionView 中显示的信息,但如何全局获取和保留该信息是一个广泛的话题。

** 编辑 **

跟进为什么懒惰的问题......

override func viewDidLoad() {
super.viewDidLoad()

let dataController:MyDataController = MyDataController()
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}

根据选择变成这样

override func viewDidLoad() {

super.viewDidLoad()
self.view.addSubview(collectionView)
}

// all other code

// then right at the bottom of your class

// MARK: - Properties

lazy var collectionView: UICollectionView = {

let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)

let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self.dataController
collectionView.delegate = self.dataController
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()

return collectionView
}()

lazy var dataController: MyDataController = {

let dataController = MyDataController()
dataController.delegate = self
// more setup if needed

return dataController
}()

我觉得这很方便,您的属性总是很容易找到、容易移动和更改。如果我注释掉 viewDidLoad 中的 addSubview 行,在这种情况下就足以停止初始化 collectionViewdataController全部一起。也许对于这个例子来说有点过分了,但从整体上采用它可以节省大量时间并生成清晰可读的 oo 代码。

关于ios - swift 。如何在 ViewController 以外的单独的 swift 文件中编写 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37009850/

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