My name is Wim Haanstra and I have a question about the AQGridView you created. The grid is awesome, but I think I am doing something wrong somewhere.
This is what I do to get the grid filled:
- I have a NSMutableArray where I put in objects (for example UIImage objects).
- When the grid executes "numberOfItemsInGridView", I respond with the following : [imageArray count] + 1. The +1 is for a "Load more" button.
- When the grid executes "cellForItemAtIndex", I take a look at which index he wants and if it is the last item (the +1 one) I create a button in that cell, with a UIButton for loading more images. Otherwise, I just create a cell with an image in there.
- When the "Load More" button is pressed, I execute a function which puts more objects in the NSMutableArray (imageArray).
What should I do after that, to make the grid show the new images? A reloadData does not work (it tries to add duplicate items then?).
The way it should work is:
- Add 3 images to NSMutableArray
- Add 4 cells to GridView, 3 for the images, 1 for the UIButton for loading more
- Press button adds 3 more images to the NSMutableArray
- Now 7 cells should be in the gridview, the first 6 for the images and 1 for the load more button.
I cannot get this to work, without crashing my application somehow.
Maybe you can help me out?
Thanks in advance,
Wim Haanstra
wim@wimhaanstra.com
Anonymous
Well, aside from the fact that -reloadData really ought to work (that sounds like a bug to me), the best way to do what you want is to use the -insertItemsAtIndices:animation: method of AQGridView. In your case, since you’re only performing a single change (that is, adding a number of items) you would just call that function once. If you were doing a few different things (adding some, moving others, deleting more) then you would call -beginUpdates and -endUpdates on the grid view before and after so that all the changes are batched together in a single transaction.
So in your case, I’d use the following code:
- (void) addMoreButtonClicked
{
// change the data store now-- it must reflect the new data
[self appendImagesToArray: 3];
// tell the grid view about the changes
// this will ultimately call your callbacks, hence changing
// the data store in advance. The store *must* match
// the expected state *after* the insert/delete/move.
NSIndexSet * added = [NSIndexSet indexSetWithIndexesInRange:
NSMakeRange([imageArray count]-3,3)];
[self.gridView insertItemsAtIndices:added
animation:AQGridViewItemAnimationFade];
}