UICollection Vea cómo deseleccionar todo
Tengo una configuración FollowVC y FollowCell con vista de colección. Puedo mostrar todos los datos correctamente en mi celda de vista uIcollection usando el siguiente código sin ningún problema.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FollowCell", forIndexPath: indexPath) as? FollowCell {
let post = posts[indexPath.row]
cell.configureCell(post, img: img)
if cell.selected == true {
cell.checkImg.hidden = false
} else {
cell.checkImg.hidden = true
}
return cell
}
}
Tenga en cuenta que también podría seleccionar y deseleccionar varias imágenes usando el siguiente código
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if deletePressed == true {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
cell.checkImg.hidden = false
} else {
let post = posts[indexPath.row]
performSegueWithIdentifier(SEGUE_FOLLOW_TO_COMMENTVC, sender: post)
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
cell.checkImg.hidden = true
}
Cuando en el modo "Seleccionar", puedo realizar la selección de cada celda y se mostrará una marca de verificación en la celda. Sin embargo, lo que quiero hacer es tener un botón de cancelar para deshabilitar todas las celdas seleccionadas y eliminar el checkImg.
Yo he tratado
func clearSelection() {
print("ClearSelection posts.count = \(posts.count)")
for item in 0...posts.count - 1 {
let indexP = NSIndexPath(forItem: item, inSection: 0)
followCollectionView.deselectItemAtIndexPath(indexP, animated: true)
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
cell.checkImg.hidden = true
}
}
El programa se bloquea aquí, lo que me da un error fatal: inesperadamente encontrado nulo al desenvolver un error opcional en
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
No sé por qué tiene problemas para desenvolver la celda para ser mi FollowCell, que contiene una instancia de checkImg. Ya lo usé antes en una situación similar en didSelectItemAtIndexPath y parece funcionar?
Gracias,