gpt4 book ai didi

ios - 如何修复此警告 : setting the first responder view of the collection view but we don't know its type (cell/header/footer)?

转载 作者:行者123 更新时间:2023-12-02 14:23:15 30 4
gpt4 key购买 nike

我正在尝试将搜索栏添加到我的 Collection View 中。我想要实现的是使用 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 过滤数据方法。

当我使用 TableView 时,一切正常。在 TableView 中,当我添加UISearchBar时使用addSubView方法,我得到了几乎相同的警告,但是当我添加 UISearchBar 时它消失了使用setTableHeaderView方法。

但是在 Collection View 中,不存在与 TableView 执行相同操作的方法。所以我添加了UISearchBar补充标题 View ,如下所示:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView;
if (kind == UICollectionElementKindSectionHeader) {
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
[reusableView addSubview:self.searchBar];
}
return reusableView;
}

当我添加UISearchBar时通过这种方式对象,我收到警告,“设置 Collection View 的第一响应者 View ,但我们不知道它的类型(单元格/页眉/页脚)?”每次我点击搜索栏时。

除了警告之外,它似乎工作正常,但是当我输入某些内容时,我遇到了问题。如果我输入一个字母,键盘输入 View 就会下降,因此我必须再次点击搜索栏才能继续搜索。如果我要搜索的单词是 5 个字母的单词,我必须点击搜索栏 4 次。太可怕了。

我认为这是由于警告而发生的。我用谷歌搜索了警告上下文,找到了 stackoverflow 上唯一与此相关的文章。(其他文章与表格 View 相关。所有这些似乎都通过使用 setTableHeaderView 来修复。)

文章是this .

但是我没有注册iOS开发者计划,所以我无法阅读那篇文章。有没有人愿意帮我解决这个问题?

最佳答案

发生这种情况是因为在 Collection View 上调用“reloadData”时会重新加载标题 View ,因此搜索栏会失去其第一响应者状态。我能找到的唯一解决方法是在事件搜索正在进行时保留一个 bool 值,如果是,则将搜索栏设置为 Collection View “cellForItemAtIndexPath”中的firstResponder:

if(activeSearch && ![searchBar isFirstResponder])
{
[searchBar becomeFirstResponder];
}

关于ios - 如何修复此警告 : setting the first responder view of the collection view but we don't know its type (cell/header/footer)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538702/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com