繁体   English   中英

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

[英]How to bind NSDictionary data to table view in objective c?

我使用下面的代码将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;
    }
)

我需要将其转换为其他吗? 或如何在UITableView中绑定以上输出?

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

签出这个简单的表格视图教程

  1. 拿一个NSMutuableArray并将dictionary添加到该array例如

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

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

json数据存储到Global Delare 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 DataSource方法。

-(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;
}

暂无
暂无

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

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