HalesEnchanted
HalesEnchanted

Reputation: 665

Call CollectionViewCell item from IBAction

I would like to add actions to a button in a toolbar I added to a collectionView.

The toolbar has 4 share buttons but some reason, I'm not able to call the collectionView items properly.

- (IBAction)share:(id)sender
{
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [_collectionView insertItemsAtIndexPaths:@[newIndexPath]];
    NSArray *selectedIndexPaths = [_collectionView indexPathsForSelectedItems];
    NSMutableArray *selectedTexts = [NSMutableArray array];
    for (NSIndexPath *indexPath in selectedIndexPaths) {
        NSArray *section = videoArray[indexPath.section];
        NSString *text = section[indexPath.row];
        [selectedTexts addObject:text];   
        SLComposeViewController *tweetSheet = [SLComposeViewController
            composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:text];
        [self presentViewController:tweetSheet animated:YES completion:nil];
        NSLog(@"%@", text);
    }
    // NSLog(@"The share works fine");
} 

This returns an error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

Upvotes: 0

Views: 87

Answers (2)

Sapozhnik Ivan
Sapozhnik Ivan

Reputation: 285

You have to update your property which contains your items. Check numberOfRowsInSection: method

EDIT: To share the text of the appropriate collection view cells you no need to insert any cells into existing collection view. try this code

- (IBAction)share:(id)sender
{
    NSArray *selectedIndexPaths = [_collectionView indexPathsForSelectedItems];
    NSMutableArray *selectedTexts = [NSMutableArray array];
    for (NSIndexPath *indexPath in selectedIndexPaths) {
        NSArray *section = videoArray[indexPath.section];
        NSString *text = section[indexPath.row];
        [selectedTexts addObject:text];
    }

    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeTwitter];
    NSString *textToShare = [selectedTexts componentsJoinedByString:@" "];
    [tweetSheet setInitialText:textToShare];
    [self presentViewController:tweetSheet animated:YES completion:nil];
    NSLog(@"%@", textToShare);
}

Upvotes: 0

Azat
Azat

Reputation: 6795

When you call -insertItemsAtIndexPaths: you should take care about dataSource methods as well because UICollectionView asks them again about updated items. In your case you don't return proper numbers of updated items count in section 0 and that leads to the error you get.

Usually you have some source of your UICollectionView and you should update it as well

Upvotes: 1

Related Questions