我是iOS的新手,在iOS中学习东西。我正在制作UITableView
和Segue的演示。我已成功实现了表视图,现在我想使用segue将值传递给另一个View Controller。我尝试过如下,但我的问题是我每次从UITableView
点击的详细信息按钮都会获得相同的记录。我不知道我的代码有什么问题。
有人能帮我解决一下吗?
我在这里发布我的代码:
码
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
// NSString *name = [dict valueForKey:@"name"];
[self performSegueWithIdentifier:@"detail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//NSLog(@"selected index path==%@",indexPath);
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}
答案 0 :(得分:2)
如下所示更改您的代码,您不需要发送self
作为发件人。发送indexPath
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"detail" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"detail"]) {
NSIndexPath *indexPath = sender;
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}