2

だから、私はiOSプログラミングで少し遊んでいます。あまり時間はありませんが、私はそれを学ぶのを延期してきました。しかし、私はこのおかしな問題で2日間立ち往生しました。collectionViewにセルを追加しようとすると、次のエラーが発生します。

'NSInternalInconsistencyException'、理由:'セクション0にアイテム0を挿入しようとしましたが、更新後、セクション0には0個のアイテムしかありません'

これが私のコードです:

@interface DocumentsViewController() {
    FileManager *fileManager;
    NSMutableArray *folderContent;
}

@end

@implementation DocumentsViewController

@synthesize fileList;

- (void)viewDidLoad
{
    [super viewDidLoad];

    folderContent = [[NSMutableArray alloc] init];
    fileManager = [[FileManager alloc] init];

    NSArray *items = [fileManager getDirectoryInfo];
    [self createFileList:items];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createFileList:(NSArray *)items
{
    for(NSString *item in items)
    {
        NSInteger count = [folderContent count];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];
        [folderContent insertObject:item atIndex:count];

        [self.fileList insertItemsAtIndexPaths:@[indexPath]];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DefaultCell" forIndexPath:indexPath];

    cell.image.image = [UIImage imageNamed:@"folder.png"];
    cell.label.text = @"oi"; //objects[indexPath.row];

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return folderContent.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

@end

よろしくお願いします、ルーカス

アップデート

さて、いくつかの非常に役立つヒントの後、別のエラーメッセージが表示されました:'NSInternalInconsistencyException'、理由:'無効な更新:セクション0のアイテムの数が無効です。更新(1)後に既存のセクションに含まれるアイテムの数は更新前にそのセクションに含まれていたアイテムの数(1)に、そのセクションから挿入または削除されたアイテムの数(1が挿入され、0が削除された)をプラスまたはマイナスし、プラスまたはマイナスで移動または移動されたアイテムの数に等しくなります。そのセクションの(0が入ってきて、0が出て行った)。

以下で説明するように、(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectionを間違って使用している可能性があります。これは、folderContent.countの値が変更される時間を与えずに、2回続けて呼び出されたためです。

4

1 に答える 1

-1

リストを作成するときに、本当にinsertItemsAtIndexPathを使用しますか?これにより、一度に1つずつアニメーション化されます。forループの後に完全に入力された後で[self.fileListreloadData]を実行してみませんか?

さらに、この行はあなたに問題を与えているかもしれないと思います- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];

UITableViewsと同じ意味の行を持たないコレクションビューを使用しています(indexPathForRowではなくindexPathForItemに注意してください) NSIndexPath *indexPath = [NSIndexPath indexPathForItem:count inSection:0];

于 2013-03-12T05:02:29.077 に答える