noobsmcgoobs
noobsmcgoobs

Reputation: 2746

UICollectionView Segues and Multiple Selection on same UICollectionViewCell

I have a UICollectionView. If I touch a cell, it triggers a segue. If "trash" or "save" is enabled, then users should be able to touch cells to add to an array that is processed for the corresponding action.

When trash/save is enabled, the segue triggers instead of allowing multiple selection. How do I do this so that I can have 2 modes: 1 for segues and 1 for multiple selection.

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (self.saveEnabled == YES) {
        NSArray *itemsToDelete = [self.collectionView indexPathsForSelectedItems];

        [self.itemsArray addObjectsFromArray:itemsToDelete];
    }

    else if (self.trashEnabled == YES) {
        NSArray *itemsToDelete = [self.collectionView indexPathsForSelectedItems];
        [self.itemsArray addObjectsFromArray:itemsToDelete];
    }
    else{
        [self performSegueWithIdentifier:@"collectionUnwind" sender:self];

    }
}

Upvotes: 1

Views: 598

Answers (2)

Fu Jiantao
Fu Jiantao

Reputation: 104

The key step is that you need return false in shouldPerformSegueWithIdentifier when in multi-selection mode.

Here is how I do this in swift:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    return !self.collectionView.allowsMultipleSelection
}

And I switch between single-select and multi-select mode with long press, you can use button to do the same.

func setupLongPressGesture() {
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.collectionView.addGestureRecognizer(longPressGesture)
}

@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
    if gestureRecognizer.state == .began {
        self.labelState.text = "multiple selection enabled"
    } else if gestureRecognizer.state == .ended {
        self.collectionView.allowsMultipleSelection = !self.collectionView.allowsMultipleSelection
    }
}

I refer to the tutorial here: https://www.appcoda.com/ios-collection-view-tutorial/

Upvotes: 1

Kujey
Kujey

Reputation: 1122

Not sure of what you really want. Tell us exactly what you do (buttons) and in which order to delete (or save) multiple cells.

By the way did you enable multiple selection on your collection view ?

[self.collectionView setAllowsMultipleSelection:YES];

EDIT :

In your storyboard, just add a segue with "collectionUnwind" identifier from your current ViewController (and not the cells of its CollectionView) to the new ViewController you want to push. If you link it to the cells, Xcode will assume you need the new ViewController to be pushed on every cell selection.

Your current code should do the rest.

Upvotes: 0

Related Questions