从iOS上的“详细信息”按钮上的UITableView获取相同的记录

时间:2015-12-28 06:22:41

标签: ios objective-c iphone uitableview

我是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];
    }
}

1 个答案:

答案 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];
    }
}