UICollectionView reloadData在节头中重新调用第一个响应者

And*_*ers 16 cocoa-touch objective-c uisearchbar ios uicollectionview

我有一个UICollectionView有节标题.在标题标题中,我有一个UISearchBar.我想在搜索栏中输入时过滤我的集合视图中的内容.我用以下方法执行此操作:

// 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];
}
Run Code Online (Sandbox Code Playgroud)

每次搜索栏文本更改时都会调用此方法.这条线[self.collectionView reloadData]为每个角色隐藏了键盘.是否可以仅重新加载UICollection视图中的数据而不重新加载补充视图,如节标题标题?

我的collectionView中的数据来自NSFetchResultController.

我很满意我的UI如何工作,所以如果有一个简单的方法不重新加载节标题,那就太棒了!

Jas*_*son 26

在尝试了许多不同的选择后,我终于弄明白了.解决方案是使您自己的部分标题,以便您可以独立重新加载其他部分而无需重新加载标题附加到的部分.

所以:

  • 第0节
      • 搜索栏(或其他文本字段)
      • (没有)
  • 第1节
      • 创建一个空的UICollectionReusableView并覆盖... sizeForItemAtIndexPath:(NSIndexPath*)indexPath返回CGSizeZero
      • 您最初使用第0部分的实际行

然后,我们需要重新加载您的数据:

[collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];

在用户键入时,您的搜索栏或文本字段应保持其焦点,您可以在后台更新结果.

  • 不幸的是,通过`sectionHeadersPinToVisibleBounds将搜索栏保持在顶部是不可能的. (2认同)

Cam*_*amo 6

您是否尝试过使用此选项之一?

仅重新加载新项目

    [self.collectionView reloadItemsAtIndexPaths:indexPaths];
Run Code Online (Sandbox Code Playgroud)

重新加载完整部分

    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
Run Code Online (Sandbox Code Playgroud)

或者,如果它不起作用.. 更新后再次使 searchBar 成为第一响应者

    [self.collectionViewPackages performBatchUpdates:^{
        [self.collectionView reloadData];
    } completion:^(BOOL finished) {
        [self.searchBar becomeFirstResponder];
    }];
Run Code Online (Sandbox Code Playgroud)