I display a list of users through a UICollectionView (stored in CoreData) Each cell has : - a name (UItextField) - a state (ON / OFF) (bool)
I want to pass my boolean var concerning the user state (ON or OFF) with a UISwitch.
I've correctly set up my collectionview func : identified my cell, displayed the name of user, added a target func for the UISwitch.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userId, for: indexPath) as! UserCell
let nameInfos = usersList[indexPath.item]
cell.usersList = nameInfos
nameInfos.name = cell.userNameTextField.text
cell.userNameTextField.delegate = self
let switchActive = cell.userSwitch
switchActive.addTarget(self, action: #selector(self.didPlayerActivate(_:)), for: .valueChanged)
return cell
}
@objc func didPlayerActivate(_ sender : UISwitch){
sender.isOn ? isActive() : isntActive()
}
// then my 2 func : isActive and isntActive
My question is how to set each user ON or OFF with my "didPlayerActivate" func. I thought about selecting the right indexPath with the UISwitch, but I don't know how to do this.