作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
- (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];
}
}
我想访问选定的行 index
,但为每个选定的行显示 null
。请帮帮我?
最佳答案
两种情况:
Segue
从 viewController
连接
调用segue
来自你的 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
无需实现 didSelectRowAtIndexPath
方法和performSegueWithIdentifier:
.你可以直接得到sender
作为UITableviewCell
在prepareForSegue:sender:
方法。
- (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];
}
}
关于ios - 如何在 prepareForSegue 中获取索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26278730/
我是一名优秀的程序员,十分优秀!