从IBAction调用CollectionViewCell项

时间:2015-05-04 20:18:09

标签: ios nsindexpath collectionview

我想在添加到button的{​​{1}}中向toolbar添加操作。

collectionView有4个共享按钮,但有些原因,我无法正确调用toolbar项。

collectionView

这会返回错误:

  

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无效更新:第0部分中的项目数无效。更新后现有部分中包含的项目数(3)必须等于更新前该部分中包含的项目数(3),加上或减去从该部分插入或删除的项目数(插入1个,删除0个),加上或减去移入的项目数或超出该部分(0移入,0移出)。'

2 个答案:

答案 0 :(得分:1)

当您致电-insertItemsAtIndexPaths:时,您应该关注dataSource方法,因为UICollectionView会再次询问有关更新项目的问题。在您的情况下,您不会在第0部分中返回正确数量的更新项目,这会导致您获得错误。

通常你有UICollectionView的某些来源,你也应该更新它

答案 1 :(得分:0)

您必须更新包含商品的商家。检查numberOfRowsInSection:方法

编辑: 要共享相应集合视图单元格的文本,您无需将任何单元格插入现有集合视图。试试这段代码

- (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);
}
相关问题