0

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).'

rmaddy
  • 314,917
  • 42
  • 532
  • 579
HalesEnchanted
  • 615
  • 2
  • 9
  • 20
  • i did not understand you completely... why are you inserting a new item at the beginning? (`insertItemsAtIndexPaths`) – André Slotta May 04 '15 at 20:23
  • @AndreSlotta Thank you for replying. I thought it made sense but since it doesn't work, it doesn't. With tableview, I usually do NSIndexPath *myIndexPath = [self.tablename indexPathForSelectedRow]; int row = [myIndexPath row]; NSString *title = [iamarray objectAtIndex:row]; and it works. I guess my question is what is the collectionview alternative?! – HalesEnchanted May 04 '15 at 20:39

2 Answers2

1

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

Azat
  • 6,745
  • 5
  • 31
  • 48
0

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);
}
Sapozhnik Ivan
  • 235
  • 3
  • 7
  • Thank you for replying, could you please tell me how to go about this?! – HalesEnchanted May 04 '15 at 20:52
  • As far as I understand the idea is to share the text of the selected collection view cells? – Sapozhnik Ivan May 04 '15 at 21:36
  • Hi, Thanks! Tried. It returns nothing :( – HalesEnchanted May 04 '15 at 22:27
  • Make sure that collection view properties allowsMultipleSelection and allowsSelection are both set to YES. Then under debug mode check that variables selectedIndexPaths and selectedTexts are not nil and has at least one value. – Sapozhnik Ivan May 05 '15 at 14:47
  • Still confused :( I tried this: - (IBAction)share:(id)sender { UIButton *button = (UIButton*)sender; NSLog(@"%@", button); NSIndexPath *indexPath = [_collectionView indexPathForCell:sender]; NSString *imagePath = [videoArray objectAtIndex:indexPath.row]; UIImage *image = [UIImage imageNamed:imagePath]; NSDictionary *dates = [videoArray objectAtIndex:indexPath.row]; NSString *tweet = [dates valueForKeyPath:@"date"]; NSLog(@"Tweet Date named: %@ at row %ld", tweet, (long)indexPath.row); and it crashes still :/ – HalesEnchanted May 05 '15 at 23:58