The first time I use UICollectionView, and I'm having some difficulties. Especially with updating and deleting cells ( NSFetchResultController focus on deleting here, I hope the reason comes), with data from NSFetchResultController . I created a user cell in the inline interface as part of the storyboard:

I have a custom subclass of UICollectionViewCell with the following properties:
@property (strong, nonatomic) IBOutlet UIButton *deleteButton; @property (strong, nonatomic) IBOutlet UITextField *textField; @property (strong, nonatomic) IBOutlet UIView *containerView; @property (strong, nonatomic) IBOutlet UIView *textFieldContainer;
In IB, I set the cell class to my custom class, associated the elements with the properties of my custom class, and set the identifier to Cell .
In my View View Collection controller, I configured the collection view and fetchResultController and related methods like this:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section]; return [sectionInfo numberOfObjects]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NoteCollectionViewCell* cell = (NoteCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; [self configureCell:cell atIndexPath:indexPath]; return cell; } - (void)configureCell:(NoteCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { cell.deleteButton.tag = indexPath.row; [cell.deleteButton addTarget:self action:@selector(deleteNote:) forControlEvents:UIControlEventTouchUpInside]; [...]
My delete action is as follows:
- (void)deleteNote:(UIButton *)sender { Note *note = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:sender.tag inSection:0]]; [[AppDelegate sharedAppDelegate].managedObjectContext deleteObject:note]; [[AppDelegate sharedAppDelegate] saveContext]; }
My update action ( UITextField delegate method) is as follows:
- (void)textFieldDidEndEditing:(UITextField *)textField { Note *note = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:textField.tag inSection:0]]; note.name = textField.text; [[AppDelegate sharedAppDelegate] saveContext]; }
The problem is as follows:
- The delete button does not always delete the correct cells / object. And sometime the object / cell will not be deleted at all.
- Sometimes the application crashes when I delete an object (SIGARBT error).
- Sometimes text appears in the wrong text box.
- Sometimes, when I delete line 0 with some text, add click "Add", a new cell is added with the same text value as the previous (deleted) cell.
Any ideas on how to solve these problems would be great!
Update
As a comment below, this log in my deleteNote action always returns 0 for the indexPath string. I donโt know if this could be the reason.
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0]; NSLog(@"indexPath: %@. Row: %d", indexPath, indexPath.row);