繁体   English   中英

目标C-将JSON数据从Tableview传递到另一个View Controller

[英]Objective C - Passing JSON data from Tableview to another View Controller

JSON数据:

({

    CustDisplayName = "John Doe";
    CustID = 2;
},
    {
    CustDisplayName = "Jane Doe";
    CustID = 21;
})

像联系人列表一样在tableview上显示数据正在运行,从单元格到其他视图的segue(Push)也在工作。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
*)indexPath {
    [self performSegueWithIdentifier:@"pushDetailView" sender:indexPath];
}

现在我通过prepareForSegue传递数据

if ([[segue identifier] isEqualToString:@"pushDetailView"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    NSLog(@"%@", indexPath);
}

日志仅显示0、1、2、3,这不是我想要的。 我不确定什么是最好的方法。

  1. 选择“行”获取CustID,然后在另一个视图的viewDidLoad上执行另一个请求?
  2. 选择“行”以获取该数组并将字符串传输到另一个视图?

我是iOS开发的新手,希望有人能为您提供帮助。

我猜你需要的是indexPath.row而不是indexPath

在目标ViewController的头文件中,定义一个名为dict的NSDictionary类型的属性。 并将您的Json数据解析到具有NSDictionary对象的数组中。

if ([[segue identifier] isEqualToString:@"pushDetailView"]) {
   UIViewController *controller = segue.destinationviewcontroller;
   controller.dict = [array objectAtIndex:indexPath.row].
}

假设您的数据源是一个填充有Customer对象的NSArray

首先,在详细信息viewController中创建一个Customer属性:

@property (nonatomic,strong) Customer customer;

然后,必须在选择一行时设置此属性。 为此,请使用prepareForSegue方法:

-(void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"pushDetailView"]) {
      DetailViewController*destination= segue.destinationviewcontroller;
      NSIndexPath* indexPath = [tableView indexPathForCell:sender];
      destination.customer = datasource[indexPath.row];
   }
}

确保在调用performSegue时将单元格设置为发送方。 确保它是从情节提要中创建序列的方法,而不是手动执行。

您可能还希望使用它轻松解析您的JSON: https : //github.com/icanzilb/JSONModel/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM