I had implemented a UITableview in ViewController. I want to display some data in the TableView which is fetched through JSON.
- (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];
}
Now, When I assign the Array to the TableView Cell it gives Array is Empty. But in the getData method Array consists of 10 elements.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tittleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*reUse=@"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;
}
Why is it So.? How can i Do it.? Can anyone provide me valid solutions?