0

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?

3
  • 1
    reloadData is missing ... .reload your tableview Commented Jul 5, 2016 at 7:58
  • Rather than three separate arrays use one array and a custom class containing the three properties as data model. This is object oriented programming. Commented Jul 5, 2016 at 8:34
  • Checkout given answer. Commented Jul 5, 2016 at 8:49

5 Answers 5

1

Well, while all other answers suggesting to add

[self.tableView reloadData];

might have solved the problem if your block was running in the main thread but since you are doing a network operation which goes to another thread, you won't be able to modify UI from that thread. You might want to do this instead:

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
});

First of all, it was a fundamental problem, where you were not even calling the reload function. Another problem was that of basic understanding of how things work. So whenever, your model is ready and filled but your UI is not changing (given that you call the function to change UI), try and see the log output, sometimes it says that you cannot change the UI in another thread and also check which thread you code is running on.

Sign up to request clarification or add additional context in comments.

Comments

0

Just reload your tableView from the completion handler of dataTaskWithURL.

And wait till completion of web service call because it is asynchronous call, so your reloaddata of tableview will get call after some times. It can take much time if data is bigger or internet connection is slow.

You can put breakpoints in completion handler to check that when it get called.

You can display activity indicator also.

Second thing make sure that your title,imageStr or imageData is not nil or null. Because you are just printing count of array. If you add empty object in array then it will increase count but it's actual value will be blank.

So, manage properly everything and you will figure out that when you are doing mistake.

Comments

0

call reload data if array count greater than 0 see below code for reference

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

 // add reload data
 if([self.termArray count] >0)
// call reload data here
 }

Comments

0

Simply write like, alloc and init object of your array object before calling API.

Like,

NSMutableArray *titleArray = [[NSMutableArray alloc] init]; 

for all array which you are using into CellForRowAtIndexPath method for showing records.

and reload your table view data once all records fetching done from web service.

Note : As per your question, you are getting null array so please be check with given solutions.

1 Comment

He also said that he is getting the count as 10 previously.
0

Reload your table inside the block

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

        }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView Reloaddata];
});

        NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
    }]; [dataTask resume];

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.