Selecting all the items in UICollectionView iOS, e

2019-01-26 14:37发布

问题:

I am in the middle of creating an application where I have a button on a tool bar which selects all the items inside a collection view.

But the problem that I am facing is, when I tap on the button it selects only those items that are visible on the screen. This is due to the CELL REUSE functionality.

Is there any way that I can select all the cells, even those which are not currently visible to the user?

Thanks J

回答1:

It is not possible to have all cells selected when using cell reuse.

Due to cell reuse the number of actual cells which exist at any moment is a couple more than the number of cells currently visible. i.e. 6 cells visible is about 8 cells existing.

You are able to find out how many visible cells there are with

NSArray *visiblePaths = [self.collectionView indexPathsForVisibleItems];

The solution is to have the selected value stored within the UICollectionView datasource and use that value for when you customise a cell inside cellForItemAtIndexPath



回答2:

This is possible even with cell reuse. You can select all cells in the first section through:

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

If you have more than 1 section, just use another nested for loop to loop through all the sections.



回答3:

I struggled thro the same problem where I wanted to display photos and allow the user to select multiple photos, with select-all and deselct-all options. It turned out that [self.collectionView selectItemAtIndexPath:] does not work for selecting all cells.

So I ended up in maintaining a separate state flag along with the data source. In my case, each cell displayed a photo, so I had to maintain a separate BOOL array, where each element represents the current selection state of the corresponding photo in the data source array.

Please see the code below: (Source: 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
    }
}


回答4:

Updated for Swift 3

updated Gasper Kolenc answer to swift 3 , for future use if anyone looking for this in swift 3 then..

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


回答5:

Swift 3 solution:

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


回答6:

I've created a simple extension on UICollectionView for selecting and deselecting:

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)
        })
    }

}