[英]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中綁定以上輸出?
要使用表視圖,您需要數組
簽出這個簡單的表格視圖教程
拿一個NSMutuableArray
並將dictionary
添加到該array
例如
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 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.