UICollectionViewCell - 如何在UIButton操作方法中选择所有项目/单元格

问题描述 投票:3回答:4

我试图在点击UICollectionViewCell之后选择所有的UIButtons。

我该怎么做呢?

ios uibutton uicollectionviewcell
4个回答
3
投票

针对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循环来遍历所有部分。


2
投票

这个解决方案适合我:

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];
    }
}

2
投票

更新为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: [])
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.