gpt4 book ai didi

iOS CollectionView 'NSInternalInconsistencyException'

转载 作者:行者123 更新时间:2023-12-03 16:48:37 26 4
gpt4 key购买 nike

所以,我正在尝试一下 iOS 编程。时间不多,但我已经推迟学习太久了。但我被这个奇怪的问题困扰了两天。当我尝试将单元格添加到我的 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 的值改变时间.

最佳答案

您真的想在创建列表时使用 insertItemsAtIndexPath 吗?这将使它们一次生成一个动画。为什么不在 for 循环后完全填充后执行 [self.fileList reloadData] ?

此外,我认为这一行可能会给您带来问题 - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];

您使用的 Collection View 没有与 UITableViews 相同意义的行(请注意 indexPathForItem 而不是 indexPathForRow) NSIndexPath *indexPath = [NSIndexPath indexPathForItem:count inSection:0];

关于iOS CollectionView 'NSInternalInconsistencyException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15353537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com