ios - 如何在 prepareForSegue 函数中搜索数据时获取 indexPath

标签 ios objective-c iphone segue uisearchcontroller

我有一个问题。
我有一个 tableView 和 searchController。
当我搜索“C”时,我得到两个名称标题有“C”的数据。
但是当我按下单元格时,detailView 会为我显示错误的信息。

NSArray *searchResult;
UISearchController *mysearchController;


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"getDetail"]) {

    if (searchResult != nil) {

        DetailViewController *detailViewController = segue.destinationViewController;

        NSIndexPath *indexPath =  [searchResult..............];
        NSDictionary *item = searchResult[indexPath.row];
        detailViewController.detailName = item[@"name"];
        detailViewController.detailLocation = item[@"address"];


    }else{

        DetailViewController *detailViewController = segue.destinationViewController;

        NSIndexPath *indexPath = self.listTableView.indexPathForSelectedRow;
        NSDictionary *item = self.InfoArray[indexPath.row];
        detailViewController.detailName = item[@"name"];
        detailViewController.detailLocation = item[@"address"];

    }
  }

}

谢谢!

最佳答案

您需要在 UITableviewdidSelectRowAtIndexPath delegate 方法中传递 Indexpath。 所以你的代码看起来像这样,

示例代码:

NSArray *searchResult;
UISearchController *mysearchController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //code here for select the cell amd put the name of the SegueIdentifier
    [self performSegueWithIdentifier:@"getDetail" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"getDetail"]) {

        if (searchResult != nil) {

            DetailViewController *detailViewController = segue.destinationViewController;

            NSIndexPath *indexPath = (NSIndexPath *) sender;
            NSDictionary *item = searchResult[indexPath.row];
            detailViewController.detailName = item[@"name"];
            detailViewController.detailLocation = item[@"address"];


        }else{

            DetailViewController *detailViewController = segue.destinationViewController;

            NSIndexPath *indexPath =  (NSIndexPath *) sender;
            NSDictionary *item = self.InfoArray[indexPath.row];
            detailViewController.detailName = item[@"name"];
            detailViewController.detailLocation = item[@"address"];

        }
    }

}

希望它有用:)

如果有任何问题,请告诉我。

关于ios - 如何在 prepareForSegue 函数中搜索数据时获取 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266294/

相关文章:

iphone - Objective-C JSON 解析错误

ios - iOS SDK 中的多个请求

ios - 来自 Nativescript CLI 的 ApplicationVerificationFailed

iphone - 架构 armv7 的 undefined symbol : "_OBJC_CLASS_$_Flurry"

iphone - 使用对象按字母顺序对 tableview 进行排序并显示按字母顺序排列的 tableview 标题,如通讯录

objective-c - 在 ios 延迟后启动 UIActivityIndi​​cator

objective-c - 如何获取 UI 日历?

iphone - 如何将声音放入数组中?

iphone - 将图像映射到不规则多边形?

iphone - CABasicAnimation 无 HUGE_VALF 无限重复?