【问题标题】:UICollectionViewCell - how to select all items/cells in UIButton action methodUICollectionViewCell - 如何在 UIButton 操作方法中选择所有项目/单元格
【发布时间】:2014-05-04 17:53:37
【问题描述】:

我正在尝试在点击 UIButton 后选择所有 UICollectionViewCells。

我该怎么做?

【问题讨论】:

    标签: ios uibutton uicollectionviewcell


    【解决方案1】:

    为 Swift 3 更新

    Just Shadow 的答案已更新至 Swift 3

      for i in 0..<assetCollectionView.numberOfSections {
        for j in 0..<assetCollectionView.numberOfItems(inSection: i) {
            assetCollectionView.selectItem(atIndexPath: IndexPath(row: j, section: i), animated: false, scrollPosition: .none)
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式选择第一部分中的所有单元格:

      for (NSInteger row = 0; row < [self.collectionView numberOfItemsInSection:0]; row++) {
          [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
      }
      

      如果您有超过 1 个部分,只需使用另一个嵌套的 for 循环来遍历所有部分。

      【讨论】:

        【解决方案3】:

        更新到 Swift 4

        更新了 Indrajit Sinh Rayjada 的答案并将其放入扩展中,因为这太笼统了,它确实应该是一个扩展。

        extension UICollectionView {
            func selectAll() {
                for section in 0..<self.numberOfSections {
                    for item in 0..<self.numberOfItems(inSection: section) {
                        self.selectItem(at: IndexPath(item: item, section: section), animated: false, scrollPosition: [])
                    }
                }
            }
        }
        

        【讨论】:

          【解决方案4】:

          解决办法如下:

          for (NSInteger i = 0; i < [_assetCollectionView numberOfSections]; i++)
          {
              for (NSInteger j = 0; j < [_assetCollectionView numberOfItemsInSection:i]; j++)
              {
                  [_assetCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-07
            • 1970-01-01
            相关资源
            最近更新 更多