UICollectionView reloadData在section标题中重新签署第一个响应者 [英] UICollectionView reloadData resigns first responder in section header

查看:307
本文介绍了UICollectionView reloadData在section标题中重新签署第一个响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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部分

    • 标题

      • 搜索(或其他文本字段)



      • (None)



        • 创建一个空的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.

        这篇关于UICollectionView reloadData在section标题中重新签署第一个响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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