当 searchBar 位于部分标题中时,UICollectionView reloadData 辞去第一响应者/关闭键盘 [英] UICollectionView reloadData resigns first responder/dismisses keyboard when searchBar is in section header

查看:19
本文介绍了当 searchBar 位于部分标题中时,UICollectionView reloadData 辞去第一响应者/关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有节标题的 UICollectionView.在部分标题中,我有一个 UISearchBar.当我在搜索栏中键入内容时,我想过滤我的收藏视图中的内容.我用以下方法做到这一点:

I have a UICollectionView that have section header. In the section header I have a UISearchBar. I want to filter the content in my collection view when I type in the search bar. I do this with the following method:

// 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];
}

每次搜索栏文本更改时都会调用此方法.[self.collectionView reloadData] 行隐藏了每个字符的键盘.是否可以仅重新加载 UICollection 视图中的数据而不重新加载补充视图(如部分标题标题)?

This method gets called every time the search bar text changes. The line [self.collectionView reloadData] hided the keyboard for every character. Is it possible to reload only the data in a UICollection view and not reload the Supplementary views like section headers headers?

我的 collectionView 中的数据来自 NSFetchResultController.

The data in my collectionView comes from a NSFetchResultController.

我对我的 UI 的工作方式非常满意,所以如果有一种简单的方法可以不重新加载部分标题,那就太好了!

I'm pretty happy with how my UI works so if there is a simple way to NOT reload the section header, then that would be great!

推荐答案

在尝试了许多不同的选项后,我终于想通了.解决方案是制作自己的部分的标题,这样您就可以独立地重新加载其他部分,而无需重新加载标题所附加的部分.

I finally figured this out after trying many different options. The solution is to make your header of its own section so you can reload the other sections independently without reloading the one your header is attached to.

所以:

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

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

        Then, we you need to reload your data:

        <代码>[collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];

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

        Your searchbar or text fields should retain their focus while the user is typing and you can update the results in the background.

        这篇关于当 searchBar 位于部分标题中时,UICollectionView reloadData 辞去第一响应者/关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆