0

I'm deleting a CollectionViewCell with this code:

- (void)deleteProjects:(NSNotification *)notification {
// Get the current project
NSString *currentProject = [[MyManager sharedManager] projectForDeletion];

[_objects removeObject:currentProject];

[_projectsCollectionView performBatchUpdates:^{

    NSArray *selectedItemsIndexPaths = [_projectsCollectionView indexPathsForSelectedItems];

    // Now delete the items from the collection view.
    [_projectsCollectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];

} completion:nil];

// Subtract 1 from editedProjects
editedProjects = editedProjects - 1;

// Set deletedProject
deletedProject = currentProject;

// Delete the associated subjects if any
[self deleteAssociatedSubjects];

// Save the new objects
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:@"myProjects"];
}

For the user to delete an item:

  • enter edit mode
  • select cell
  • select a red x that'll appear
  • it'll get deleted

Now if the user selects 2 cells, he/she will get notified that only 1 is editable at a time - but the items still get's "selected" - causing the delete to delete the item that couldn't be edited instead of the original one. How can I fix this?

Method for selecting:

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

Thanks!

1 Answers1

0

That indexPathForSelectedItem looks troublesome.

You can probably do this instead to get the indexPath:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[test indexOfObject:currentProject] inSection:0];

[_objects removeObject:currentProject];

[_projectsCollectionView performBatchUpdates:^{

    [_projectsCollectionView deleteItemsAtIndexPaths:@[indexPath]];

} completion:nil];

...
Zhang
  • 11,549
  • 7
  • 57
  • 87
  • Hmm. Thought i tried that but got an exception. Ill try later and keep you updated! – user3680475 Jun 19 '14 at 06:40
  • If I try that this exception gets thrown: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete section 1, but there are only 1 sections before the update'" @Zhang – user3680475 Jun 19 '14 at 13:39
  • Did you put section 0 or section 1? The error says you're trying to delete in section 1 instead of section 0. – Zhang Jun 19 '14 at 14:24
  • Pls help me with this question: http://stackoverflow.com/questions/24532712/how-to-delete-custom-cell-from-uicollectionview @Zhang – user3680475 Jul 02 '14 at 16:34