Ios 如何在objective c中将NSDictionary数据绑定到表视图?

Ios 如何在objective c中将NSDictionary数据绑定到表视图?,ios,objective-c,uitableview,nsdictionary,Ios,Objective C,Uitableview,Nsdictionary,我使用以下代码将JSON数据(从SOAP服务)转换为NSDictionary -(void)retriveFromSYSoapTool:(NSMutableArray *)_data{ NSLog(@"data: %@",_data); NSArray *value = [_data valueForKey:@"GetDemoDataResult"]; NSError *error; NSData *objData = [value[0] dataUsingEnc

我使用以下代码将JSON数据(从SOAP服务)转换为
NSDictionary

-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
    NSLog(@"data: %@",_data);
    NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
    NSError *error;
    NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",json);
}
输出

2017-04-04 13:03:51.085 SoapService[18444:588594] (
        {
        firstName = "1 Jhon";
        lastName = Macay;
    },
        {
        firstName = "2 William";
        lastName = Proter;
    },
        {
        firstName = "3 Joe";
        lastName = Root;
    },
        {
        firstName = "4 Brendon";
        lastName = Haejoy;
    },
        {
        firstName = "5 Jhon";
        lastName = Snow;
    },
        {
        firstName = "6 Theon";
        lastName = Greyjoy;
    }
)
我需要把这个转换成其他的吗?或者如何将上述输出绑定到UITable中​风景

  • 取一个
    NSMutuableArray
    并将
    dictionary
    添加到此
    数组中

    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [array addObject: json];
    
    //Than Reload Tableview
    
    Note: Declare array Globally to access in your class
    
  • tableview
    显示数据,如

    cell.label.text = [[array objectAtIndex:indexPath.row]valueForKey:@"firstName"];
    
  • 取一个
    NSMutuableArray
    并将
    dictionary
    添加到此
    数组中

    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [array addObject: json];
    
    //Than Reload Tableview
    
    Note: Declare array Globally to access in your class
    
  • tableview
    显示数据,如

    cell.label.text = [[array objectAtIndex:indexPath.row]valueForKey:@"firstName"];
    

  • 要使用表视图,需要使用数组


    签出简单表视图教程要使用表视图,需要使用数组


    签出简单表视图教程

    json数据存储到Global Declare NSArray

    -(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
        NSLog(@"data: %@",_data);
        NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
        NSError *error;
        NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"%@",json);
        DataArray = [NSArray arrayWithObjects:json, nil];
        [tableView reloadData];
    }
    
    这里,DataArray是全局声明的NSArray对象

    现在编写UITableView数据源方法

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return DataArray.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jsoncell" forIndexPath:indexPath];
        NSDictionary *dict = DataArray[indexPath.row];
        NSString *output = [NSString stringWithFormat:@"%@ %@",dict[@"firstName"],dict[@"lastName"]];
        cell.textLabel.text = output;
        return cell;
    
    }
    

    json数据存储到Global Declare NSArray

    -(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
        NSLog(@"data: %@",_data);
        NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
        NSError *error;
        NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"%@",json);
        DataArray = [NSArray arrayWithObjects:json, nil];
        [tableView reloadData];
    }
    
    这里,DataArray是全局声明的NSArray对象

    现在编写UITableView数据源方法

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return DataArray.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jsoncell" forIndexPath:indexPath];
        NSDictionary *dict = DataArray[indexPath.row];
        NSString *output = [NSString stringWithFormat:@"%@ %@",dict[@"firstName"],dict[@"lastName"]];
        cell.textLabel.text = output;
        return cell;
    
    }
    
    应该是这样的

    在.h文件中声明jsonArray

    @property (strong, nonatomic)  NSMutableArray *jsonArray;
    
    添加以下行viewDidLoad

    self.jsonArray = [[NSMutableArray alloc]init];
    
    
     -(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
    
        NSLog(@"data: %@",_data);
    
        NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
    
        NSError *error;
    
        NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
    
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
    
        NSLog(@"%@",son);
    
       [self.jsonArray addObject:[[json objectForKey:@"firstname"]stringByAppendingString:[json objectForKey:@"lastname"]];
    
       [tableViewName reloadData];
    
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.jsonArray.count;
    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"cell"];
    
    }
    
    cell.textLabel.text = [self.jsonArray objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    应该是这样的

    在.h文件中声明jsonArray

    @property (strong, nonatomic)  NSMutableArray *jsonArray;
    
    添加以下行viewDidLoad

    self.jsonArray = [[NSMutableArray alloc]init];
    
    
     -(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
    
        NSLog(@"data: %@",_data);
    
        NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
    
        NSError *error;
    
        NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
    
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
    
        NSLog(@"%@",son);
    
       [self.jsonArray addObject:[[json objectForKey:@"firstname"]stringByAppendingString:[json objectForKey:@"lastname"]];
    
       [tableViewName reloadData];
    
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.jsonArray.count;
    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"cell"];
    
    }
    
    cell.textLabel.text = [self.jsonArray objectAtIndex:indexPath.row];
    
        return cell;
    }
    

    NSDictionary没有顺序,因此将字典附加到数组,并在tableview中加载数组,最后访问字符串ike cell.textlabel.text=yourarrayname[indexpath.row][@“firstName”];我相信您的JSON实际上是一个数组,而不是一个字典。您可以通过使用调试器检查json,或者通过记录对象的类来进行检查。将其分配给
    NSArray*
    ,然后在tableview数据源中使用。NSDictionary没有顺序,因此将字典附加到数组,并在tableview中加载数组,最后访问字符串ike cell.textlabel.text=yourarrayname[indexpath.row][@“firstName”];我相信您的JSON实际上是一个数组,而不是一个字典。您可以通过使用调试器检查json,或者通过记录对象的类来进行检查。将其分配给
    NSArray*
    ,然后在tableview数据源中使用它。