gpt4 book ai didi

ios - 搜索结果 TableView 未被选中

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:44 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个充满单元格的 TableView。一切正常——当我点击一个单元格时,它会立即调用 tableView:DidSelectRowAtIndexPath: 并执行 segue,将我带到下一个屏幕。我还有一个搜索栏,使用 UISearchDisplayController,允许用户搜索 tableView 中的项目。当我在搜索栏中键入一些文本时,与搜索匹配的单元格会显示在表格中。

现在,我的问题是当我点击此搜索结果表格中显示的单元格之一时...在第一次点击时,表格 View 没有任何响应。如果只是短暂地按住点击,单元格会变成灰色,就像它被选中一样,但是 tableView:DidSelectRowAtIndexPath: 仍然没有被调用,并且在释放点击后单元格会恢复正常。但如果我长按几秒钟,tableView:DidSelectRowAtIndexPath: 最终会被调用,我会被带到正确的屏幕。

有没有人遇到过这个问题?据我所知,我的 UISearchDisplayController 实现方式与以往完全相同,而且从未遇到过这个问题。

谢谢,如果我能提供任何可能有帮助的额外信息,请告诉我

编辑

我不确定问题到底出在哪里,所以我不确定要显示哪些方法,但这里有一些代码......我在单击 UINavigationBar 中的图标时调出搜索栏,然后在编辑完成后将其从 super View 中删除。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

NSArray *contentArray;
if (tableView == self.tableView) {
contentArray = self.postArray;
} else if (tableView == self.searchDisplayController.searchResultsTableView) {
contentArray = self.searchResults;
}

// This is pretty hackish, but it wasn't working before for some reason. So I send the PFObject I want as the sender
[self performSegueWithIdentifier:@"ShowPostDetails" sender:contentArray[indexPath.row]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";

PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

[self updateSubviewsForCell:cell inTableView:tableView atIndexPath:indexPath];
/*
if (tableView == self.searchDisplayController.searchResultsTableView) {
[cell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)]];
} */

return cell;
}

#pragma mark - Search Bar Delegate

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"text contains[c] %@", searchText];
self.searchResults = [self.postArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];

return YES;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[self.view addSubview:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.center = CGPointMake(self.view.window.center.x, 42);
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[self.searchDisplayController.searchBar removeFromSuperview];
}

最佳答案

我发现了我的问题...

遇到此问题的特定 View Controller 是包含这些委托(delegate)方法的 View Controller 的子类,并包含用于输入信息的 UITextField。我观察 keyboardDidAppear 通知,当它出现时,我将 UITapGestureRecognizer 添加到 View 以通过在点击 View 时退出 UITextField 的第一响应者来关闭键盘。当我添加这个时,我还没有实现搜索功能,所以我知道键盘弹出的唯一原因是 UITextField。问题在于,当为搜索栏弹出键盘时添加的这个额外的 TapGestureRecognizer 阻止了 UITableView 单元格中内置的 TapGestureRecognizer 触发。我在错误的地方寻找问题。为了解决这个问题,我只是在添加手势识别器之前检查了 UITextField 是否确实是第一响应者。现在一切正常。

所以对于遇到类似问题的任何其他人,我会说回去并确保您没有任何其他可能与 tableView 的手势识别器冲突的 UIGestureRecognizers。

感谢所有发表评论的人。帮助我找到了问题所在。

关于ios - 搜索结果 TableView 未被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397525/

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