Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Alice is a view controller that has a collection view. Alice populates her collection view with data from a managed object. Alice also has a button that triggers a modal segue to Bob, who takes input from the user and gives it to Alice. I want Alice to add this input to her collection view, but I don't want her collection view to reload any data until that data is dumped from the device's memory.
 
It turns out that collection views has a method called insertItemsAtIndexPaths, which takes an array of index paths. Now, I'm trying to figure out why I'm getting an NSInternalInconsistencyException. Here's the reason:
attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update


At this point, there are supposed to be 0 items before the user inputs any data. Here's some code from Alice's .m file:

Code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name;
    NSInteger entityIndex = [indexPath row];
    @try {
         Entity *entity = [[coreDataObject entities] objectAtIndex:entityIndex];
         name = [entity name];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception in cellForRowAtIndexPath: %@",[exception reason]);
    }
    UICollectionViewCell *cell = [[UICollectionViewCell alloc] init];
    UIView *contentView = [cell contentView];
    // Create the label that will display the given username.
    UILabel *label = [[UILabel alloc] init];
    [label setText:name];
    [label setFont:[UIFont fontWithName:@"Arial" size:60]];
    // Put the label in the collection view cell's content view.
    [contentView addSubview:label];
    // Return the cell.
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger entityCount = [[coreDataObject userEntities] count];
    return entityCount;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger entityIndex = [indexPath row];
    Entity *entity = [[coreDataObject userEntities] objectAtIndex:userEntityIndex];
}

-(void)nameInputted:(NSString *)name
{
    UICollectionView *collectionView = [self collectionView];
    NSInteger rowNum = [collectionView numberOfItemsInSection:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:rowNum inSection:0];
    [collectionView insertItemsAtIndexPaths:@[indexPath]];
    [self dismissViewControllerAnimated:false completion:nil];
}
 
It seems that the solution to the exception was to make sure that my numberOfItemsInSection method doesn't return 0 after the collection view is updated.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.