简体   繁体   中英

How to bind NSDictionary data to table view in objective c?

I have used the below code to convert the JSON data(from SOAP service) to 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);
}

Output

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

Do I need to convert this to any other? or how could I bind the above output in UITable​View?

To work with table view you need array

Checkout this simple table view tutorial

  1. take one NSMutuableArray and add dictionary to this array like

     NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject: json]; //Than Reload Tableview Note: Declare array Globally to access in your class 
  2. tableview display data like

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

Store json data into 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];
}

Here DataArray is Globally Declare NSArray Object;

Now Write UITableView DataSource Method.

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

}

It should be like this

Declare jsonArray in your .h file

@property (strong, nonatomic)  NSMutableArray *jsonArray;

Add below line 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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