![](/img/trans.png)
[英]how can i create object of collectionview Cell in delegate sizeForItemAtIndexPath in swift
[英]CollectionView cell delegate swift
我有帶有 collectionView 和 UICollectionViewDataSource 擴展的 ViewController:
class CheckInViewController: UIViewController {
let checkInSystem = CheckInSystem()
let collectionView = UICollectionView()
viewDidAppear(){
super.viewDidAppear(animated)
checkInSystem.lala() // its function which should call delegate
}
//...
}
extension CheckInViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CheckInCell.identifier, for: indexPath) as! CheckInCell
}
還有協議:
protocol AnimateTheLastCheckInDayDelegate: AnyObject{
func animateLastDay(_ cell: CheckInCell)
}
這是實現
extension CheckInViewController: AnimateTheLastCheckInDayDelegate{
func animateLastDay(_ cell: CheckInCell) {
cell.backgroundColor = .red
print(1233254234) //works
}
class CheckInSystem{
weak var animateTheLastCheckInDayDelegate: AnimateTheLastCheckInDayDelegate?
func checkInScreenLaunched(){
//...
func lala(){
animateTheLastCheckInDay?.animateLastDay(CheckInCell())
}
}
我需要從animateTheLastCheckInDay?.animateLastDay(CheckInCell())更改單元格背景。 但它創造了新的細胞。 怎么修??
這將創建一個新的單元格CheckInCell()
animateTheLastCheckInDay?.animateLastDay(CheckInCell())
您需要傳遞單元格,因此為委托方法willDisplay
添加實現
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
checkInSystem.lala(cell)
}
和
func lala(_ cell: CheckInCell)
animateTheLastCheckInDay?.animateLastDay(cell)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.