Я реализовал UITableview
в ViewController
. Я хочу отобразить некоторые данные в TableView
, который извлекается через JSON
.TableView Array возвращает null
- (void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
}
Теперь, когда я назначаю Array ячейке TableView, он дает Array Empty. Но в методе getData массив состоит из 10 элементов.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tittleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*[email protected]"use";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reUse];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUse];
cell.textLabel.textColor = [UIColor orangeColor];
}
cell.textLabel.text=[self.tittleArray objectAtIndex:indexPath.row];
cell.imageView.image=[self.imageArray objectAtIndex:indexPath.row];
return cell;
}
Почему это так.? Как мне это сделать.? Может ли кто-нибудь предоставить мне правильные решения?
reloadData отсутствует ... .reload your tableview –
Вместо трех отдельных массивов используется один массив и пользовательский класс, содержащий три свойства в качестве модели данных. Это объектно-ориентированное программирование. – vadian
Оформить заказ на указанный ответ. –