I'm new to swift and I'm trying to make a particular subview which is a collectionViewcell selected through calling a function inside the parent collectioncell.
I am calling setChosenToBeTrue()
which sets the isChosen variable to be true in the subview, but for some weird reason the variable's value doesn't really get set? What can be the issue? Thanks! I looked at some resources that passes data through story board or there are some old ones that are not in swift and I'm still confused.
Here's my code
In the parent collectioncell
,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let rightCell = collectionView.dequeueReusableCell(withReuseIdentifier: rightCell, for: indexPath) as! RightCell
rightCell.setChosenToBeTrue()
return rightCell
}
In the subview
var isChosen: Bool = false
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setChosenToBeTrue() {
self.isChosen = true //function is called but setting it here,
//when it reaches setUpViews(), this boolean is still false
}
func setupViews(){
self.isChosen = true // setting it here works (obviously)
print(isChosen)
backgroundColor = UIColor.gray
}