UICollectionView reloadData在节头中退出第一个响应者
我有一个UICollectionView
有部分标题。 在部分标题中,我有一个UISearchBar
。 我想要在search栏中input我的collections夹视图中的内容。 我用下面的方法做到这一点:
// The method to change the predicate of the FRC - (void)filterContentForSearchText:(NSString*)searchText { NSString *query = searchText; if (query && query.length) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ or createdAt contains[cd] %@ or noteText contains[cd] %@ or keywords contains[cd] %@", query, query, query, query]; [self.fetchedResultsController.fetchRequest setPredicate:predicate]; } else { [self.fetchedResultsController.fetchRequest setPredicate:nil]; } NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { // Handle error NSLog(@"Error"); } [self.collectionView reloadData]; }
每次search栏文本更改时都会调用此方法。 该行[self.collectionView reloadData]
每个字符的键盘。 是否有可能重新加载只在UICollection视图中的数据,而不是重新加载补充意见像部分标题头?
我的collectionView中的数据来自NSFetchResultController。
我很满意我的用户界面如何工作,所以如果有一个简单的方法不重新加载部分标题,那就太棒了!
我尝试了很多不同的select后,终于明白了这一点。 解决的办法是制作自己的部分的头部,这样你就可以独立地重新加载其他部分,而无需重新加载头部所附的部分。
所以:
- 第0部分
- 头
- search栏(或其他文本字段)
- 行
- (没有)
- 头
- 第一节
- 头
- 创build一个空的UICollectionReusableView并重写… sizeForItemAtIndexPath:(NSIndexPath *)indexPath返回CGSizeZero
- 行
- 您的实际行本来将与第0节
- 头
那么,我们你需要重新加载你的数据:
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
您的search栏或文本字段在用户input时应保持其焦点,并且可以在后台更新结果。
你有尝试过这个选项吗?
重新加载新的项目
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
重新加载完整的部分
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
或者,如果它不工作..更新后,再次searchBar第一响应
[self.collectionViewPackages performBatchUpdates:^{ [self.collectionView reloadData]; } completion:^(BOOL finished) { [self.searchBar becomeFirstResponder]; }];