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

查看:269
本文介绍了当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天全站免登陆