UISearchbarController返回错误的索引路径?

时间:2014-08-03 14:46:42

标签: ios uitableview uisearchresultscontroller

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

所以继承我的代码:

行数

- (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(我尝试使用推送连接但是当我选择searchresult Cell时甚至没有得到推送视图......)

- (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,怎么回事?我需要有人帮忙

1 个答案:

答案 0 :(得分:0)

如果将搜索显示控制器的resultsTableView委托设置为此View Controller,则结果TableView将查找didSelectRowAtIndexPath。

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

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

相关问题