我正在尝试使用从JSON请求接收的对象填充tableView。
这是我的viewDidLoad方法:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog (@"HE ENTRADO EN HISTORIAL VC");
myObject = [[NSMutableArray alloc] init];
NSURL *apiURL = [NSURL URLWithString:
[NSString stringWithFormat:@"http://.. HIDDEN HERE.. /?client=%@&date=%@", @"1",@"2015-3-16"]];
NSURLRequest *request = [NSURLRequest requestWithURL:apiURL];
NSLog (@"HE MANDADO LA REQUEST DEL HISTORIAL");
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(data.length) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(responseString && responseString.length) {
NSLog(@"dATOS RECIBIDOS EN HISTORIAL=%@", responseString);
NSLog (@"HE RECIBIDO LA REQUEST DEL HISTORIAL");
NSError *jsonError;
NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
NSArray *messageArray = [json objectForKey:@"objects"];
historialServicios = [[NSMutableArray alloc]init];
// Parse and loop through the JSON
for (dictionary in messageArray) {
//datos de nivel objects
NSString * code = [dictionary objectForKey:@"code"];
NSString * date = [dictionary objectForKey:@"date"];
//datos de nivel client
NSDictionary *level2Dict = [dictionary objectForKey:@"client"];
id someObject = [level2Dict objectForKey:@"email"];
NSLog(@"ANTES DE ANADIR OBJETOS");
[historialServicios addObject:@{@"code": code, @"date": date, @"email":someObject}];
NSLog(@"DESPUES DE ANADIR OBJETOS");
NSLog(@"NOMBRE===%@",someObject);
NSString * email = someObject;
NSLog(@"EMAIL=%@",email);
NSLog(@"CODE=%@",code);
NSLog(@"DATE=%@",date);
//insertamos objetos en diccionario historialServicios
}
NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++SERVICIOS RECIBIDOS=%@", historialServicios);
}
}
}];
NSLog(@"NUMERO DE ITEMS=%lu", (unsigned long)[historialServicios count]);
[self.tableView reloadData];
}
最后一个NSLog没有出现在日志控制台中。 NSMutableArray的内容显示在控制台中:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++SERVICIOS RECIBIDOS=(
{
code = SV1000000103;
date = "2015-03-18";
email = "jose@gmail.com";
},
{
code = SV1000000113;
date = "2015-03-18";
email = "jose@gmail.com";
},
{
code = SV1000000104;
date = "2015-03-16";
email = "jose@gmail.com";
},
...
但桌子上没有显示任何内容。
这里有tableView方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return historialServicios.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *email = [[historialServicios objectAtIndex:indexPath.row] valueForKey:@"email"];
NSLog(@"VALOR DEL EMAIL=%@", email);
cell.detailTextLabel.text = email;
return cell;
}
我做错了什么?
答案 0 :(得分:1)
你应该在" SERVICIOS RECIBIDOS =%@"之后从完成块内部调用[self.tableView reloadData]
。线。
像在您之前一样在块外调用它,会使表重新加载它,然后再从服务器接收它。
希望它能帮到你,amigo。