【问题标题】:How to Segue-Push after using UISearchBar in a TableView, project with storyboard如何在 TableView 中使用 UISearchBar 后进行 Segue-Push,带有情节提要的项目
【发布时间】:2012-05-20 18:20:59
【问题描述】:

我在一个使用情节提要的项目中有一个带有 UISearchBar 的 UITableView。

搜索功能没问题。

我将单元格链接到 Seague 推送到下一个视图。

如果我选择一个单元格,则会加载下一个视图并且一切正常。

一切都很完美,直到我使用 UISearchBar,然后停止工作 Segue Push,当我选择过滤的单元格时,我无法转到下一个视图。

怎么办?强制转场。

此链接上的项目模型:

http://uploaded.to/file/ooohrxnf

//Code for search
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

[searchData removeAllObjects];

NSArray *group;

for(group in arraynumbers)
{
    NSMutableArray *newGroup = [[NSMutableArray alloc] init];
    NSString *element;

    for(element in group)
    {
        NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];

        if (range.length > 0) {
            [newGroup addObject:element];
        }
    }

    if ([newGroup count] > 0) {
        [searchData addObject:newGroup];
    }        
}

onTableData = searchData;

return YES;
}

// Force Segue when UISearchBar isActive
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchDisplayController.isActive) {


    NSMutableArray *mutablerray = [searchData objectAtIndex:indexPath.section];        
    number = [mutablerray objectAtIndex:indexPath.row];       
    [self performSegueWithIdentifier:@"detail" sender:self];
    searchBar.text = @"";
}

}

// pickup number when UISearchBar isNotActive and reloadData after Segue
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"detail"])
{
    if (!self.searchDisplayController.isActive) 
    {

        number = [[onTableData objectAtIndex:[[self.tableView indexPathForSelectedRow] section]] objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
    }

    NSLog(@"number: %@", number);
    [segue.destinationViewController setNumber:number];

    [self.tableView reloadData];
}
}

【问题讨论】:

    标签: storyboard push tableview uisearchbar segue


    【解决方案1】:

    我遇到了完全相同的问题,并最终尝试了各种方法,因为 UISearchDisplayController 对我来说非常新。我最终将 tableView 设置为 searchDisplayController 的 searchResultsDelegate:

    searchDisplayController.searchResultsDelegate = self;
    

    我不太清楚为什么这会照顾它。有人解释一下吗?

    【讨论】:

    • 在答案中添加代码时,请添加 ' 或四个空格。 '' 或四个空格 .
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2019-08-04
    • 1970-01-01
    相关资源
    最近更新 更多