Ios UICollectionView如何取消选择所有
我有一个FollowVC和FollowCell设置与集合视图。我可以使用以下代码将所有数据正确显示到uIcollection视图单元格中,没有问题Ios UICollectionView如何取消选择所有,ios,objective-c,swift,uicollectionview,uicollectionviewcell,Ios,Objective C,Swift,Uicollectionview,Uicollectionviewcell,我有一个FollowVC和FollowCell设置与集合视图。我可以使用以下代码将所有数据正确显示到uIcollection视图单元格中,没有问题 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { if let cell = collectionView.dequeueR
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
}
}
注意,我还可以使用以下代码选择和取消选择多个图像
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
}
在“选择”模式下,我可以对每个单元格进行选择,单元格上将显示一个复选标记。但是,我想做的是有一个取消按钮来禁用所有选定的单元格并删除checkImg
我试过了
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
}
}
程序在此崩溃,这给了我一个致命错误:在打开一个可选错误时意外地发现了nil
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
我不知道为什么它在将包含checkImg实例的单元格展开为我的FollowCell时遇到问题。我以前已经在didSelectItemAtIndexPath的类似情况下使用过它,它似乎可以工作吗
谢谢,当您清除选择状态时,并非所有选定的单元格都在屏幕上,因此
collectionView.cellForItemAtIndexPath(indexPath)
可能返回零。因为你有一个强制向下转换,在这种情况下你会得到一个例外
您需要修改代码以处理潜在的nil
情况,但也可以通过使用UICollectionView的indexPathsForSelectedItems
属性来提高代码的效率
let selectedItems = followCollectionView.indexPathsForSelectedItems
for (indexPath in selectedItems) {
followCollectionView.deselectItemAtIndexPath(indexPath, animated:true)
if let cell = followCollectionView.cellForItemAtIndexPath(indexPath) as? FollowCell {
cell.checkImg.hidden = true
}
}
在swift4中使用扩展
extension UICollectionView {
func deselectAllItems(animated: Bool) {
guard let selectedItems = indexPathsForSelectedItems else { return }
for indexPath in selectedItems { deselectItem(at: indexPath, animated: animated) }
}
}
为了进一步简化,您可以这样做
followCollectionView.allowsSelection = false
followCollectionView.allowsSelection = true
事实上,这将正确地清除您的followCollectionView.indexPathsForSelectedItems,即使它感觉非常错误。此答案在swift 4.2中可能很有用
let selectedItems = followCollectionView.indexPathsForSelectedItems
for (value in selectedItems) {
followCollectionView.deselectItemAtIndexPath(value, animated:true)
if let cell = followCollectionView.cellForItemAtIndexPath(value) as? FollowCell {
cell.checkImg.hidden = true
}
}
我这样做更容易解决问题:
tableView.selectRow(at: nil, animated: true, scrollPosition: UITableView.ScrollPosition.top)
谢谢这正是我所需要的,而且有效。不过,我不需要使用任何重载项代码。我照你说的做了,它的工作原理和你说的一模一样。或者,如果你更喜欢它的话:tableView.indexPathsForSelectedRows?.forEach({tableView.deselectRow(at:$0,animated:false)})
Awesome,帮助我在tableView中实现了类似的功能。
collectionView.indexPathsForSelectedItems?
.forEach { collectionView.deselectItem(at: $0, animated: false) }