UICollectionVer como desmarcar tudo
Eu tenho um FollowVC e FollowCell Setup com a coleção View. Posso exibir todos os dados corretamente na minha célula de visualização uIcollection usando o código a seguir sem nenhum 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
}
}
Observe que eu também poderia selecionar e desmarcar várias imagens usando o seguinte 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
}
Quando no modo "Selecionar", eu posso executar a seleção de cada célula e uma marca de seleção será exibida na célula. No entanto, o que eu quero fazer é ter um botão de cancelamento para desativar toda a célula selecionada e remover o checkImg.
eu tentei
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
}
}
O programa trava aqui, causando um erro fatal: Inesperadamente encontrado nulo ao desembrulhar um erro opcional em
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
Não sei por que está tendo problemas para desembrulhar a célula para ser o meu FollowCell, que contém uma instância do checkImg. Eu já o usei antes em uma situação semelhante no didSelectItemAtIndexPath e parece funcionar?
Obrigado,