AlanQuatermain / AQGridView

A grid view for iPhone/iPad, designed to look similar to NSCollectionView.
http://quatermain.tumblr.com/
BSD 3-Clause "New" or "Revised" License
2.37k stars 447 forks source link

Using AQGridView with an NSFetchedResultsController #98

Open awmwong opened 12 years ago

awmwong commented 12 years ago

I'm having some troubles getting the the gridView to work with a FRC, primarily with the FRC delegates for inserting/updating/deleting elements.

These delegate functions provide an indexPath for the tableView functions, but AQGV expects indexSets for its insert/delete/update functions.

Could you offer some assistance?

(and yes I do realize I can call a reloadData for the entire gridView on didChangeContent, but I'd prefer a nicer solution if possible.)

evadne commented 12 years ago

If there is only one section (no section key path), the row index can be used as the grid index. Methinks.

On Dec 3, 2011, at 3:34 AM, Anthony Wong wrote:

I'm having some troubles getting the the gridView to work with a FRC, primarily with the FRC delegates for inserting/updating/deleting elements.

These delegate functions provide an indexPath for the tableView functions, but AQGV expects indexSets for its insert/delete/update functions.

Could you offer some assistance?

(and yes I do realize I can call a reloadData for the entire gridView on didChangeContent, but I'd prefer a nicer solution if possible.)


Reply to this email directly or view it on GitHub: https://github.com/AlanQuatermain/AQGridView/issues/98

awmwong commented 12 years ago

Hmm, good call.

I'll give that a shot and get back..

awmwong commented 12 years ago

So creating an indexSet using [NSIndexSet indexSetWithInt[indexPath row]], kinda works?

I'm getting error messages thrown around though when the FRC starts notifying about changes.. Visible cell list is missing some items!

indicies missing

And eventually will crash with an EXC_BAD_ACCESS.

Pah! Anyone else with ideas?

evadne commented 12 years ago

Post it. :)

On Dec 3, 2011, at 5:36 AM, Anthony Wong reply@reply.github.com wrote:

So creating an indexSet using [NSIndexSet indexSetWithInt[indexPath row]], kinda works?

I'm getting error messages thrown around though when the FRC starts notifying about changes.. Visible cell list is missing some items!

indicies missing

And eventually will crash with an EXC_BAD_ACCESS.

Pah! Anyone else with ideas?


Reply to this email directly or view it on GitHub: https://github.com/AlanQuatermain/AQGridView/issues/98#issuecomment-2996192

awmwong commented 12 years ago

So heres the FRC delegate...

#pragma mark - NSFetchedResultsController delegates
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

    [self.gridView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.gridView insertItemsAtIndices:[NSIndexSet indexSetWithIndex:[indexPath row]] withAnimation:AQGridViewItemAnimationNone];

            break;

        case NSFetchedResultsChangeDelete:
            [self.gridView deleteItemsAtIndices:[NSIndexSet indexSetWithIndex:[indexPath row]] withAnimation:AQGridViewItemAnimationNone];
            break;

        case NSFetchedResultsChangeUpdate:
            [self.gridView reloadItemsAtIndices:[NSIndexSet indexSetWithIndex:[indexPath row]] withAnimation:AQGridViewItemAnimationNone];
            break;

        case NSFetchedResultsChangeMove:
            [self.gridView deleteItemsAtIndices:[NSIndexSet indexSetWithIndex:[indexPath row]] withAnimation:AQGridViewItemAnimationNone];
            [self.gridView insertItemsAtIndices:[NSIndexSet indexSetWithIndex:[newIndexPath row]] withAnimation:AQGridViewItemAnimationNone];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

    [self.gridView endUpdates];
}
#pragma mark -
#pragma mark Grid View Data Source

- (NSUInteger) numberOfItemsInGridView: (AQGridView *) aGridView
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.resultsController sections] objectAtIndex:0];

    return [sectionInfo numberOfObjects];
}
jonruiz commented 12 years ago

I'm having the same problem. I did notice one thing in your code - on insertions, use newIndexPath instead of indexPath (the latter is nil on insertions). Though I don't think that will solve your problem. Please post if you figured out what's wrong.

joshpaul commented 12 years ago

Out of curiosity, does this solve the problem for you?

https://github.com/samsoffes/ssfetchedresultscontroller

jonruiz commented 12 years ago

I have had to put this on hold for a while, but when I get back to it I will try your suggestion and let everyone know if/how it works. Thanks!

deeje commented 12 years ago

I'm using fetchedResultsController with an AQGridView, and generate indices with NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

deeje commented 12 years ago

and related, here's how I generate indexSets inside didChangeObject:

NSIndexSet  *indexSet = nil;
if (type == NSFetchedResultsChangeInsert)
    indexSet = [NSIndexSet indexSetWithIndex:newIndexPath.row];
else
    indexSet = [NSIndexSet indexSetWithIndex:indexPath.row];
jonruiz commented 12 years ago

Thanks deeje and joshpaul - once I can get this project off the back-burner, I have two good suggestions and I'll report back on the results!