【问题标题】:How are cells deselected programmatically in UICollectionView when allowMultipleSelection is enabled?启用 allowMultipleSelection 时,如何在 UICollectionView 中以编程方式取消选择单元格?
【发布时间】:2013-01-21 15:39:18
【问题描述】:

我在集合视图中启用了 allowMultipleSelection。细胞改变它们的 点击时选定的状态。都好。但是,当我想使用下面的代码将整个视图重置为选定状态:否时,单元格似乎完全取消选择,直到我进行新的选择,此时所有先前选择的单元格都显示其先前选择的状态。

即尽管出现,当我以编程方式取消选择单元格时,collectionview 并未更新其当前选择列表

- (void)clearCellSelections {
   for (LetterCell  *cell in self.collectionView.visibleCells) {
        [cell prepareForReuse];
    }
}

在自定义单元格中:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self setSelected:NO];
}

我做错了什么?还有其他方法可以取消选择所有单元格吗?

感谢 TBlue 观看

【问题讨论】:

  • 刷新collectionview控件?

标签: iphone objective-c uicollectionview


【解决方案1】:

你可以遍历- [UICollectionView indexPathsForSelectedItems]:

for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

【讨论】:

  • 小修正。有一个animated 参数。因此它应该是:[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
  • 编辑动画参数。
  • 你还需要self.collectionView.reloadData()
  • 如果没有 reloadData() 只有可见的单元格会被更新。
  • @Scooter 你能给我指路吗?我没有得到任何效果。
【解决方案2】:

取消选择UICollectionView 中所有选定单元格的最简单方法是将nil 作为第一个参数传递给collectionView.selectItem(at:, animated:, scrollPosition:)。例如,

collectionView.selectItem(at: nil, animated: true, scrollPosition: [])

将清除当前选择状态,即使allowsMultipleSelection == true

【讨论】:

  • 这是一个更好、更简洁的答案。但是我认为,如果您想取消选择一个部分中的所有单元格(并保持其他部分不变),您仍然需要遍历单元格。
  • 简短的回答
【解决方案3】:

您可以说UITableViewCell.selected 仅设置单元格及其内容的“可见状态/外观”。您可以通过遍历 tableView 的所有 indexPaths 并为每个单元格调用 deselectRowAtIndexPath:animated: 来取消选择单元格。

例如:

for (int i=0; i < self.myData.count; i++) {
    [self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:YES];
}

编辑:我完全同意@BenLings 和@JeremyWiebe 的cmets,@qorkfiend 的解决方案比这个更受欢迎。

【讨论】:

  • @fguchelaar 如何让它在 UICollectionView 中工作。它在那里不起作用。我试过这种方式 [self.collectionView didDeselectItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
  • @qorkfiend 的回答比这个更可取
  • 同意@BenLings,尤其是当您的数据集很大时。使用此解决方案,您必须枚举每个项目,如果您将 NSFetchedResultsController 与 Core Data 一起使用,情况会变得更糟。
【解决方案4】:

以防万一这是 Swift 中的简单解决方案:

extension UICollectionView {
    func deselectAllItems(animated animated: Bool = false) {
        for indexPath in self.indexPathsForSelectedItems() ?? [] {
            self.deselectItemAtIndexPath(indexPath, animated: animated)
        }
    }
}

【讨论】:

    【解决方案5】:

    对于 swift 3 扩展程序如下所示:

    import UIKit
    
    extension UICollectionView {
        func deselectAllItems(animated: Bool = false) {
            for indexPath in self.indexPathsForSelectedItems ?? [] {
                self.deselectItem(at: indexPath, animated: animated)
            }
        }
    }
    

    【讨论】:

      【解决方案6】:

      如果你想委托也被调用,这就完成了

      for (NSIndexPath *indexPath in [self.cuisineCollection indexPathsForSelectedItems]) {
                  [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
                  [collectionView.delegate collectionView:cuisineCollection didDeselectItemAtIndexPath:indexPath];
              }
      

      【讨论】:

        【解决方案7】:

        我创建了一个名为 toggleCellSelection 的全局变量,然后在 didSelectItemAt 函数中运行它:

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("select cell \(indexPath.row)")
        
            let cell = collectionView.cellForItem(at: indexPath)
            if (toggleCellSelection == true) {
                toggleCellSelection = false
                cell?.layer.borderWidth = 0
                cell?.layer.borderColor = UIColor.clear().cgColor
            } else {
                toggleCellSelection = true
                cell?.layer.borderWidth = 5
                cell?.layer.borderColor = #colorLiteral(red: 0.8779790998, green: 0.3812967837, blue: 0.5770481825, alpha: 1).cgColor
            }
        
        
        }
        

        【讨论】:

          【解决方案8】:

          并不是说这个答案一定是“最好的”,但由于没有人提到它,我会添加它。

          您可以简单地调用以下代码。

          collectionView.allowsSelection = false
          collectionView.allowsSelection = true
          

          【讨论】:

            【解决方案9】:

            这是@qorkfiend 在 Swift 中的回答

            // this is an array of the selected item(s) indexPaths
            guard let indexPaths = collectionView.indexPathsForSelectedItems else { return }
            
            // loop through the array and individually deselect each item
            for indexPath in indexPaths{
                collectionView.deselectItem(at: indexPath, animated: true)
            }
            

            【讨论】:

              猜你喜欢
              • 2017-03-04
              • 1970-01-01
              • 2021-08-24
              • 1970-01-01
              • 1970-01-01
              • 2021-09-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多