【问题标题】:How to get indexPath when I search data in the function that prepareForSegue在prepareForSegue的函数中搜索数据时如何获取indexPath
【发布时间】:2016-09-01 08:20:49
【问题描述】:

我有一个问题。
我有一个 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"];

    }
  }

}

谢谢!

【问题讨论】:

  • 显示完整代码NSIndexPath *indexPath = [searchResult..............];
  • [theTableViewUsedToShowSearchResults indexPathForSelectedRow] 不起作用?
  • 你能显示执行segue的代码和你的searchResult数组初始化吗?
  • @imbeginner_sorry 如果我的回答,那么请正确感谢我的努力,接受正确的答案。

标签: ios objective-c iphone segue uisearchcontroller


【解决方案1】:

需要在UITableviewdidSelectRowAtIndexPathdelegate方法中传递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"];

        }
    }

}

希望它有效:)

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

【讨论】:

  • 谢谢你的建议,但我用这个又错了。
  • 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ListTableViewCell 行]:无法识别的选择器发送到实例 0x7ff9928a4400”
  • didSelectRowAtIndexPath 我相信,你通过了 indexpath 而不是 indexpath.row?对吗?
  • 你的异常点在哪里停止,我的意思是说你在哪个方法中崩溃了?
  • 我添加异常断点。然后我停止在方法-prepareforsegue
猜你喜欢
  • 2020-03-21
  • 2015-03-05
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
相关资源
最近更新 更多