【问题标题】:reloadData not working for UICollectionView in iOS7reloadData 不适用于 iOS7 中的 UICollectionView
【发布时间】:2014-04-16 17:18:23
【问题描述】:

所以基本上我有搜索方法“searchTableList”,得到我想要的结果后,我想重新加载 uicollectionview

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   // NSLog(@"Text change - %d",isSearching);

    //Remove all objects first.
    [filteredContentList removeAllObjects];

    if([searchText length] != 0) {
        isSearching = YES;
        [self searchTableList];
    }
    else {
        isSearching = NO;
    }

    [self.collectionView reloadData];


}

但是在“[self.collectionView reloadData]”之后什么都没有发生!

使用后,一直崩溃!

[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
[self.collectionView reloadData];

我该怎么办,非常感谢!!! :D

错误信息是:

    2014-04-16 18:24:35.684 SampleProject1[59602:60b] the item width must be less that the width of the UICollectionView minus the section insets left and right values.
2014-04-16 18:24:37.709 SampleProject1[59602:60b] *** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:3688
2014-04-16 18:24:37.713 SampleProject1[59602:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update'

已修复:

基本上,我返回了一个未使用数组的计数,该数组的长度始终为 0,而不是相关数组!

所以我不得不改变这个:

if (isSearching) {

    return [searchResult count];

} else {
    return [self.tracks count];
}

到: if (isSearching) {

        return [filteredContentList count];

    } else {
        return [self.tracks count];
    }

然后在此之后我删除了所有 reloadData 术语并将其替换为:

[self.collectionView reloadData];

非常感谢大家!!

【问题讨论】:

  • 如果崩溃,错误信息是什么?
  • @MarcMosby 刚刚更新了代码和错误信息 :)
  • Google 您的最后一条错误消息。只有原因文本。
  • 代理分配是否正确?
  • @MarcMosby 非常感谢!!!!!

标签: ios objective-c cocoa-touch uikit uicollectionview


【解决方案1】:
  • 写完reloadItemsAtIndexPaths后就不用再写了 重新加载数据。
  • 使用主线程重新加载相应的集合视图行。

代码片段:

[[NSOperationQueue  mainQueue]  addOperationWithBlock:^{

    // Reload the respective collection view row using the main thread.

    [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];

}];

【讨论】:

  • 崩溃日志是什么?和之前一样吗?
  • 非常感谢兄弟!终于修好了。在完成您的解决方案后,我发现我返回 0 这是因为我使用了一个未使用的数组。我多么愚蠢:) 非常感谢您抽出宝贵的时间!
  • @Pizzy213codes,我可以做你的妹妹 :)。 M女:)
  • 我们如何在 swift 3 中做到这一点?
【解决方案2】:

检查 willDisplayCell... 方法中的任何 [collectionView reloadData] 命令。我在那里有这个并且它工作过一次,但是当我需要使用新数据集再次重新加载数据时,它就不会再触发 willDisplayCell 调用了。

一切都在主线程上,这不是问题。更像是溢出问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多