我有一个问题。
我有一个 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"];
}
}
}
谢谢!
最佳答案
您需要在 UITableview
的 didSelectRowAtIndexPath
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/