ios - 使用 UICollectionView 时 UITableView 的 NSInternalInconsistencyException?

标签 ios xcode uitableview insert uicollectionview

我试图用这段代码将项目插入到 Collection View 中:

- (IBAction)addCards:(UIButton *)sender {
    int numberOfCardsToAdd = EXTRA_CARDS_NUMBER;
    if ([[self.collectionView visibleCells] count] == 0) {
        numberOfCardsToAdd = self.startingCardCount;}

    // loop through the game model, draw random cards from the deck and assign it to the array
    // take into account the "no cards left in deck" case

    for (int i = 0; i < numberOfCardsToAdd; i++) {
        if (![self.game.deck isEmpty]) {
            SetsCard *cardToAdd = (SetsCard *) [self.game.deck drawRandomCard];
            if (cardToAdd) [self.game.cards addObject:cardToAdd];

            NSIndexPath *newObject = [[NSIndexPath alloc] initWithIndex:[self.game.cards indexOfObject:cardToAdd]];

            [self.collectionView insertItemsAtIndexPaths:@[newObject]]; // adding card to view
        }
    }
    [self.collectionView reloadData];
}

我得到了这个错误:

Assertion failure in -[NSIndexPath row], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewSupport.m:2680
2013-06-13 09:09:12.443 Improved Matchismo[1206:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.'

*** First throw call stack:
(0x1cb1012 0x10eee7e 0x1cb0e78 0xb84665 0x29eb6c 0x54b14d 0x53fd69 0x53e60e 0x53e646 0x926e 0x1102705 0x362c0 0x36258 0xf7021 0xf757f 0xf66e8 0x65cef 0x65f02 0x43d4a 0x35698 0x1c0cdf9 0x1c0cad0 0x1c26bf5 0x1c26962 0x1c57bb6 0x1c56f44 0x1c56e1b 0x1c0b7e3 0x1c0b668 0x32ffc 0x269d 0x25c5)
libc++abi.dylib: terminate called throwing an exception

这让我很困惑,因为我将项目添加到 UICollectionView,而不是 UITableView,而且我的应用程序不使用 UITableView 的任何实例> 完全没有。有谁知道我为什么会收到此错误以及如何解决?

非常感谢!

最佳答案

Collection View 与两级索引路径(部分/行)的工作方式相同 一个 TableView ,所以你应该创建索引路径

NSIndexPath *newObject = [NSIndexPath indexPathForRow:... inSection:0];

如果只有一个部分(第 0 部分)。

-[NSIndexPath row]@interface NSIndexPath (UITableView) 类别中声明,并且在 iOS 6 中引入 Collection View 时错误消息可能没有更改,因此消息“用于 UITableView 的索引路径无效”具有误导性。

关于ios - 使用 UICollectionView 时 UITableView 的 NSInternalInconsistencyException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17088216/

相关文章:

ios - 多选模式下的 UITableView 删除所选单元格中所有 subview 的背景颜色

ios - UITableViewCell 中的行仅显示来自 USGS 的第一个索引数组

iphone - 从 NSMutableData 中提取数据

ios - iOS:从发布日期开始获取weekOfMonth

Xcode 4.1 无法重新安装。应用商店显示 "installed"

xcode - 如何在 Xcode 4 中使用 dylib 文件创建工作框架

objective-c - UITableView 单元格中的 UITextField 返回 null

ios - 自动布局 : Set height View depends on other height View

ios - 在 Xcode 中成功编译和构建后,应用程序崩溃

ios - 使用 UIPageViewController 向左滑动返回第一页