reloadData不适用于iOS7中的UICollectionView

时间:2014-04-16 17:18:24

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

所以基本上我有搜索方法“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 jargons并将其替换为:

[self.collectionView reloadData];

非常感谢你们!

2 个答案:

答案 0 :(得分:4)

  • 编写reloadItemsAtIndexPaths后,无需再次写入 reloadData。
  • 使用主线程重新加载相应的集合View行。

代码段:

[[NSOperationQueue  mainQueue]  addOperationWithBlock:^{

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

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

}];

答案 1 :(得分:0)

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

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