0

我正在使用 searchBardisplayController 处理表格视图,但是当我键入搜索时,这会显示正确的结果,但是当我选择单元格时会显示不同的选择(例如,如果我在表格视图正常时选择一个单元格,则为正常的索引路径)

所以这是我的代码:

行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // NSLog(@"%i", [[InfoWebDoctores sharedInstance]numberOfInfo]);

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [displayObject count];
    } else  {
        return [allObject count];
    }

}

配置单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"docCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"docCell"];
    }



    return cell;
}

进行搜索:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if([searchString length] == 0)
    {
        [displayObject removeAllObjects];
        [displayObject addObjectsFromArray:allObject];
    }
    else
    {
        [displayObject removeAllObjects];
        for(NSDictionary *tmpDict in allObject)
        {
            NSString *val = [tmpDict objectForKey:doctorname];
            NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];

            NSString *val2 = [tmpDict objectForKey:doctoresp];
            NSRange r2 = [val2 rangeOfString:searchString options:NSCaseInsensitiveSearch];


            if(r2.location != NSNotFound || r.location != NSNotFound)
            {
                [displayObject addObject:tmpDict];
            }
        }
    }
    return YES;
}

最后将数据发送到 detailViewcontroller(我尝试使用推送连接,但是当我选择搜索结果单元格时甚至没有得到推送视图......)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailTableViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detailController"];


    NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
    //NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    int row = [index row];

    EntryJsonDoctores *entry = [[InfoWebDoctores sharedInstance]entryAtIndex:row];

    detail.modal = @[entry.nombre,entry.especialidad, entry.especialidad2, entry.especialidad3, entry.cel, entry.mail, entry.tel1, entry.tel2, entry.ext,entry.hospital,entry.direccion];


    [self presentViewController:detail animated:YES completion:^{

    }];
}

所以我尝试使用 tableview 中的 indexpathforselectedrow 和 searchresultscontroller 中的 indexpath,有什么问题?我需要别人的帮助

4

1 回答 1

0

如果将搜索显示控制器的 resultsTableView 的委托设置为这个 View Controller,那么结果 TableView 将寻找 didSelectRowAtIndexPath。

您可以在 viewDidLoad 中执行此操作并将self.searchDisplayController.searchResultsTableView.delegate = self

替换NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];NSIndexPath *index = indexPath;

于 2014-08-03T15:07:42.443 回答