Search code examples
iosipadaqgridview

AQGridView insertItemsAtIndices Items Overlapping


When I call insertItemsAtIndices on an AQGridView, the items are overlapping, rather than being displayed in their relevant grid. It works fine if I call reloadData on the GridView, but I'd like to just add one cell at a time.

I have portraitGridCellSizeForGridView implemented, but it doesn't seem to be getting called after an insert call.

Here's the relevant code to insert:

[self.cartItemArray addObject:cartItem];

[self.cartGridView beginUpdates];

[self.cartGridView insertItemsAtIndices:[NSIndexSet indexSetWithIndex:self.cartItemArray.count - 1] withAnimation:AQGridViewItemAnimationNone];

[self.cartGridView endUpdates];

Here's the AQGridView delegate methods:

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView {

return _cartItemArray.count;

}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index {

static NSString * GridCellIdentifier = @"CartItemListViewCell";

CartItemListViewCell_iPad * cell = (CartItemListViewCell_iPad *)[gridView dequeueReusableCellWithIdentifier:GridCellIdentifier];

if (!cell) {
    CGRect cellFrame = CGRectMake(0, 0, _cellWidth, _cellHeight);
    cell = [[CartItemListViewCell_iPad alloc] initWithFrame:cellFrame reuseIdentifier:GridCellIdentifier];
}

CartItem *cartItem = [_cartItemArray objectAtIndex:index];

[(CartItemListViewCell_iPad*)cell updateCartItem:cartItem];

return cell;

}


- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView {

return CGSizeMake(_cellWidth, _cellHeight);
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index {

if (index >= _cartItemArray.count){
    return;
}

self.selectedItemIndex = index;

[[NSNotificationCenter defaultCenter] postNotificationName:NotificationEditPrintSizeCartItem object:[_cartItemArray objectAtIndex:index]];

}

And here's what it looks like:

Example of cells overlapping

As you can see I'm using two AQGridView's on the screen. I wonder if that's causing the problem? Any help is appreciated. Thanks!


Solution

  • Ok, just solved it. Searching through the code, I noticed the following was called in reloadData:

    [_gridData setDesiredCellSize: [_dataSource portraitGridCellSizeForGridView: self]];
    

    So, now I'm calling reloadData before I add anything to it.