How do I add a collection view cell to a dynamically filled collection view?

Discussion in 'iOS Programming' started by moonman239, Jul 10, 2014.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    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.
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    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:

    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];
    }
    
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    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.
     

Share This Page