gpt4 book ai didi

ios - 不能在额外的类中外包 UICollectionViewDatasource

转载 作者:可可西里 更新时间:2023-11-01 01:50:38 25 4
gpt4 key购买 nike

我有一个带有 CollectionView 的 ViewController,我想将 UICollectionViewDataSource 放在一个额外的类中,以便它可以被重用。 (我没有使用界面生成器)。

因此,我创建了一个额外的类:

class MyDataSource:  NSObject, UICollectionViewDelegate, UICollectionViewDataSource {

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYIDENTIFIER", for: indexPath)
cell.backgroundColor = .blue
return cell
}

}

在带有 CollectionView 的 ViewController 中,我设置了它:

override func viewDidLoad() {
super.viewDidLoad()

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MYIDENTIFIER")

let myDataSource = MyDataSource()
collectionView.delegate = myDataSource
collectionView.dataSource = myDataSource

}

但是,collectionView 将保持为空 - 上面没有单元格。

但是,如果我将所有数据源函数放入 ViewController 类本身并将委托(delegate)和数据源设置为 self,那么一切正常。

是否不能将 UICollectionViewDataSource 外包到其他文件中,还是您还需要做其他事情?

最佳答案

以防万一。这就是我说调用 init 方法的原因。 @Andrey Chernukha,如果造成任何混淆,我们深表歉意。这就是为什么我说它与 init 一起工作,但我想这与 Pascal 的原因相同

import UIKit

class ViewController: UIViewController {

lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .green
return cv
}()

var dataSource : CustomDataSource!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.addSubview(collectionView)
collectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
dataSource = CustomDataSource(collectionView: collectionView)
}

}

class CustomDataSource : NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

let collectionView : UICollectionView

init(collectionView: UICollectionView) {
self.collectionView = collectionView
super.init()
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell")
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .red
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}

}


class Cell : UICollectionViewCell {

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

func setupViews() {
self.backgroundColor = .red
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

我测试了它,原因是 dataSource 在 collectionView 中很弱,所以如果在 viewDidLoad() 中声明,它会在实例化后立即被释放

关于ios - 不能在额外的类中外包 UICollectionViewDatasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54522909/

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