gpt4 book ai didi

ios - 自定义 UICollectionView 数据源和委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 08:04:38 26 4
gpt4 key购买 nike

由于我对 XCode 和 Swift 比较陌生,因此我已经为此苦苦挣扎了几个小时。

我的 Storyboard中有一个 CollectionView,我想将它的数据源和委托(delegate)方法链接到一个单独的类,而不是我的 ViewController,但它不工作。谁能帮忙?

override func viewDidLoad() {
super.viewDidLoad()

//
self.card.center = CGPoint(x: self.view.center.x, y: self.view.center.y)

self.card.layer.cornerRadius = 5

self.card.layer.shadowOpacity = 0.1

//

self.card2.center = CGPoint(x: self.view.center.x, y: self.view.center.y)

self.card2.layer.cornerRadius = 5

self.card2.layer.shadowOpacity = 0.1

//

self.view.bringSubview(toFront: self.card)

// HERE IS THE LINK

setDS()

collectionView.reloadData()
// ----------------------




}

private func setDS() {

let dataSourceAndDelegate = CollectionViewController()

collectionView.dataSource = dataSourceAndDelegate
collectionView.delegate = dataSourceAndDelegate


}


import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.red

print("View did load")


// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 3
}

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

// Configure the cell
cell.backgroundColor = UIColor.blue

return cell
}



// MARK: UICollectionViewDelegate


// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}



// Uncomment this method to specify if the specified item should be selected
override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}



// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return false
}

override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return false
}

override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {

}


}

最佳答案

不要使用 UICollectionViewController 的子类作为自定义 Collection View 的数据源和委托(delegate)。

而是使用简单的 NSObject 类。这样你就只需要实现数据源和委托(delegate)方法,而不必担心 UIViewcontroller 的 View 方法(你根本不需要它们)。

但即使您提供了 UICollectionViewController 的对象,它也应该可以工作。它不起作用,因为您没有在 ViewController 类中保留该对象,并且它正在自动释放。 UICollectionView 不保留委托(delegate)和数据源以防止保留周期。

let dataSourceAndDelegate = CollectionViewController()

使 dataSourceAndDelegate 成为存储属性。

此外,您需要在 ViewController 类中注册您的单元格(因为它具有您正在使用的 Collection View )。请记住 UICollectionViewController 中的 collectionView 属性与 ViewController 中的 collectionView 不同。它是一个存储属性,因为 UICollectionViewController 带有一个 colectionview。

private let reuseIdentifier = "Cell"

class ViewController: UIViewController {
let dataSourceAndDelegate = CollectionViewController()
@IBOutlet var collectionView:UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()

self.setDS()
collectionView.reloadData()

}


private func setDS() {
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

collectionView.dataSource = dataSourceAndDelegate
collectionView.delegate = dataSourceAndDelegate

}

}

关于ios - 自定义 UICollectionView 数据源和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45523768/

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