Question

In my app(Using Storyboards FYI) I have a viewController which contains a tableView. My viewController hits an API and returns an array of items that my tableView should populate with. The problem is, after successfully retrieving the array, the reloadData method does not trigger a call to cellForRowAtIndexPath. I've set the delegate and datasource properly, and still have no luck. Here is my code:

in my viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [[CCHTTPClient sharedClient] getItemsWithSuccess:^(NSArray *items) {

        self.items = items;
        [self.tableView reloadData];

    } failure:^(NSError *error) {
    }];
}

and also in my numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.items count];
}

I've also found that if i did an API call on the previous VC and set the array of the viewController's items before the viewController is pushed, the table populates properly. really unsure about what's going on here. Any help would be gratefully appreciated !

Was it helpful?

Solution

self.tableView should be an outlet if your controller and table view are made in a storyboard. You're creating a different table view in viewDidLoad with alloc init, so you're calling reloadData on that one, not the one you have on screen. Remove that alloc init line, and connect the outlet in IB.

OTHER TIPS

I would suspect this is caused by calling reloadData from a secondary thread. Try this:

[[AGHTTPClient sharedClient] getItemsWithSuccess:^(NSArray *items) {
    self.items = items;

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
} failure:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top