ios 如何在 prepareForSegue 中获取索引路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26278730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get indexpath in prepareForSegue
提问by Pintu Rajput
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Action"])
{
NSIndexPath *indexPath = [self.tbl indexPathForSelectedRow];
SecondViewController *destViewController = segue.destinationViewController;
destViewController.getString = [getArray objectAtIndex:indexPath.row];
}
}
i wanna to access the selected row index,but show nullfor every selected row. please help me?
我想访问选定的行index,但显示null每个选定的行。请帮我?
回答by Suhail kalathil
Two cases:
两种情况:
Segueconnected from theviewControllerCall
seguefrom yourdidSelectRowAtIndexPathmethod, passindexPathassender-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"Action" sender:indexPath]; }Then you can get indexPath as sender in
prepareForSegue:sender:method- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Action"]) { NSIndexPath *indexPath = (NSIndexPath *)sender; SecondViewController *destViewController = segue.destinationViewController; destViewController.getString = [getArray objectAtIndex:indexPath.row]; } }segue connected from the cell
No need to implement
didSelectRowAtIndexPathmethod andperformSegueWithIdentifier:.You can directly getsenderasUITableviewCellinprepareForSegue:sender:method.- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Action"]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; SecondViewController *destViewController = segue.destinationViewController; destViewController.getString = [getArray objectAtIndex:indexPath.row]; } }
Segue从连接viewControllersegue从您的didSelectRowAtIndexPath方法调用,传递indexPath为sender-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"Action" sender:indexPath]; }然后你可以在
prepareForSegue:sender:方法中获取 indexPath 作为发送者- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Action"]) { NSIndexPath *indexPath = (NSIndexPath *)sender; SecondViewController *destViewController = segue.destinationViewController; destViewController.getString = [getArray objectAtIndex:indexPath.row]; } }从单元格连接的segue
不需要实现
didSelectRowAtIndexPathmethod和performSegueWithIdentifier:。可以直接getsenderasUITableviewCellinprepareForSegue:sender:method。- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Action"]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; SecondViewController *destViewController = segue.destinationViewController; destViewController.getString = [getArray objectAtIndex:indexPath.row]; } }
回答by tonethar
Swift 3.0 / iOS 10
斯威夫特 3.0 / iOS 10
tableView.indexPathForSelectedRowwas introduced in iOS 9
tableView.indexPathForSelectedRow在 iOS 9 中引入
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableView.indexPathForSelectedRow{
let selectedRow = indexPath.row
let detailVC = segue.destination as! ParkDetailTableVC
detailVC.park = self.parksArray[selectedRow]
}
}
回答by onmyway133
Like this
像这样
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
FTGDetailVC *detailVC = (id)segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
FTGNote *note = self.notes[indexPath.row];
[detailVC updateWithNote:note];
}
回答by Alessio Campanelli
sorry, i don't understand what you want to do.
it's possible get the indexPath.rowthrough this method of UITAbleViewDelegatethat it's called when you tap the cell of your tableView:
对不起,我不明白你想做什么。有可能获得indexPath.row通过这种方法UITAbleViewDelegate,它当你点击你的tableView的细胞被称为:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
self.myVariable = indexPath.row
}
and then you can access to this value in the prepareForSeguein this way:
然后你可以通过prepareForSegue这种方式访问这个值:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//do something with self.myVariable
}
I hope i helped you.
我希望我帮助了你。

