selectUICollectionView iOS中的所有项目,甚至是不可见的单元格

我正在创build一个应用程序,在工具栏上有一个button,用于select集合视图中的所有项目。

但是我面对的问题是,当我点击button时,只select屏幕上可见的项目。 这是由于CELL REUSEfunction。

有什么办法可以select所有的单元格,即使是那些目前不可见的单元格?

谢谢J

使用信元重用时不可能select所有单元。

由于小区重用,在任何时刻存在的实际小区的数量比当前可见小区的数量多一对。 即6个可见细胞存在约8个细胞。

你可以找出有多less个可见的单元格

NSArray *visiblePaths = [self.collectionView indexPathsForVisibleItems]; 

解决scheme是将selected值存储在UICollectionView数据源中,并使用该值来定制cellForItemAtIndexPath

即使使用单元格,这也是可能的。 您可以通过以下方式select第一部分的所有单元格:

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

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

我努力想要显示照片的同样的问题,并允许用户select多个照片,全选和全部选项。 原来,[self.collectionView selectItemAtIndexPath:]不适用于select所有单元格。

所以我最终保留了一个单独的状态标志以及数据源。 在我的情况下,每个单元显示一张照片,所以我不得不维护一个单独的BOOL数组,其中每个元素表示数据源数组中相应照片的当前select状态。

请参阅下面的代码:(来源: http : //heliumapps.weebly.com/blog/uicollectionview-and-cell-selection )

 @interface PhotosViewController() { BOOL *selectedStates; //cell selection states boolean array [C-language array] /* Let us assume that we have an array for data source, named "myDataSourceArray". For each element in the array, the correspomding element in selectedStates represents the selection state of the cell. */ } //load data source -(void)loadDataSource { //init the data source array (myDataSourceArray) and populate it with data //free up the previously allocated memory for selecteStates, if any (in case you reuse this view controller) if(selectedStates) { free(selectedStates); selectedStates = nil; } //initilaize the selection states of all cells to NO selectedStates = malloc(myDataSourceArray.count*sizeof(BOOL)); for(NSUInteger i = 0; i < self.myDataSourceArray.count; i++) { selectedStates[i] = NO; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.reuseIdentifier forIndexPath:indexPath]; //populate cell with required data & images to display [cell setPhoto:myDataSourceArray[indexPath.item]]; //configure cell for selection state [cell configureCellForSelectionState:selectedStates[indexPath.item]]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self toggleSelectionOfCellAtIndex:indexPath]; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(nonnull NSIndexPath *)indexPath { [self toggleSelectionOfCellAtIndex:indexPath]; } -(void)toggleSelectionOfCellAtIndex:(NSIndexPath*)indexPath{ MyCollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; selectedStates[indexPath.item] = !selectedStates[indexPath.item]; [cell configureForSelectionState]; } //method to select/deselect all photos - (void) setStateForAllPhotos:(BOOL)flag { for(NSUInteger i = 0; i < self.myDataSourceArray.count; i++) { selectedStates[i] = flag; } [self.collectionView reloadData]; //on main thread } /* below method in the cell's implementation */ -(void) configureForSelectionState:(BOOL)flag { if(flag) { //configure cell for selected state [cell.layer setBorderColor:[UIColor greenColor].CGColor]; [cell.layer setBorderWidth:2]; } else { //configure cell for deselected state } } 

更新了Swift 3

更新的加斯珀Kolenc回答迅速3,为未来使用,如果任何人在迅速3寻找这然后..

  for row in 0..<self.collectionView!.numberOfItems(inSection: 0) { self.collectionView!.selectItem(at: IndexPath(row: row, section: 0), animated: false, scrollPosition: .none) } 

Swift 3解决scheme:

 stride(from: 0, to: collectionView.numberOfItems(inSection: yourSectionIndex), by: 1) .forEach{ collectionView.selectItem(at: IndexPath(row: $0, section: yourSectionIndex), animated: true, scrollPosition: []) } 

我在UICollectionView上创build了一个简单的扩展来select和取消select:

 extension UICollectionView { /// Iterates through all sections & items and selects them. func selectAll(animated: Bool) { (0..<numberOfSections).flatMap { (section) -> [IndexPath]? in return (0..<numberOfItems(inSection: section)).flatMap({ (item) -> IndexPath? in return IndexPath(item: item, section: section) }) }.flatMap { $0 }.forEach { (indexPath) in selectItem(at: indexPath, animated: true, scrollPosition: []) } } /// Deselects all selected cells. func deselectAll(animated: Bool) { indexPathsForSelectedItems?.forEach({ (indexPath) in deselectItem(at: indexPath, animated: animated) }) } }