使用UICollectionView时UITableView的NSInternalInconsistencyException异常? [英] NSInternalInconsistencyException for UITableView when using UICollectionView?

查看:101
本文介绍了使用UICollectionView时UITableView的NSInternalInconsistencyException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用以下代码将项目插入到集合视图中:

I was trying to insert items into a collection view with this code:

- (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实例.有谁知道我为什么收到此错误,以及如何解决该错误?

which really confuses me because I was adding items to an UICollectionView, not an UITableView, and my app does not use any instance of UITableView at all. Does anyone know why I got this error, and how I can fix it?

非常感谢!

推荐答案

集合视图使用两级索引路径(节/行)的方式与 表格视图,因此您应该创建索引路径

A collection view works with two-level index paths (section/row) in the same way as a table view, so you should create the index path

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

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

if there is only one section (section 0).

-[NSIndexPath row]@interface NSIndexPath (UITableView)类别中声明,并且在iOS 6中引入集合视图时,错误消息可能未更改,因此消息与UITableView一起使用的无效索引路径"具有误导性.

-[NSIndexPath row] is declared in the @interface NSIndexPath (UITableView) category and the error message has probably not been changed when collection views were introduced in iOS 6, so the message "Invalid index path for use with UITableView" is misleading.

这篇关于使用UICollectionView时UITableView的NSInternalInconsistencyException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆