Reputation: 1905
I have a UICollectionViewCell
with an item Desk
. Desk has a Bool
property that I want to change when a cell is tapped.
I'm trying to better familiarize myself with the delegate pattern, and want to avoid using something like a transparent button overlaid on the cell. I thought it would work to assign the cell's delegate in cellForItemAt
and trigger the delegate method in didSelectItemAt
, but the cell's delegate is nil when I check in didSelectItemAt
.
Struct:
struct Desk: Equatable, Codable {
var name: String
var wasTouched: Bool = false
}
Protocol:
protocol TouchTheDesk: AnyObject {
func youTouchedIt(cell: DeskCell)
}
Cell:
import UIKit
class DeskCell: UICollectionViewCell {
@IBOutlet weak var deskLbl: UILabel!
weak var delegate: TouchTheDesk?
var desk: Desk? {
didSet {
updateViews()
}
}
func updateViews() {
self.deskLbl.text = desk?.name
}
}
Conform VC to Protocol and define delegate method:
extension NotAShoppingListVC: TouchTheDesk {
func youTouchedIt(cell: DeskCell) {
if cell.desk?.wasTouched != nil {
cell.desk!.wasTouched = !cell.desk!.wasTouched
} else {
cell.desk!.wasTouched = true
}
collectionView.reloadData()
}
}
cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? DeskCell else {return UICollectionViewCell()}
cell.desk = deskDealer.desks[indexPath.item]
if cell.desk!.wasTouched { //assigned on line above
cell.backgroundColor = .red
} else {
cell.backgroundColor = .green
}
cell.delegate = self
return cell
}
didSelectItemAt:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? DeskCell else {return}
#warning("delegate is nil")
cell.delegate?.youTouchedIt(cell: cell)
}
edit: If I call the delegate method directly in didSelectItemAt
and pass in the cell, the indexPath for the cell is nil in the delegate method
Upvotes: 1
Views: 242
Reputation: 100503
Inside didSelectItemAt
Replace
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? DeskCell else {return}
with
guard let cell = collectionView.cellForItem(at: indexPath) as? DeskCell else {return}
when you use dequeueReusableCell
outside of cellForItemAt
it will return a different cell other than the clicked one
Upvotes: 2