I need to set my UITableView delegate and datasource from a separate class (data ready after parsing called by a method), but every time my table is emtpy. I'm using ARC and this is simplified code:
//HomeViewController.h
#import <UIKit/UIKit.h>
#import "TableController.h"
@interface HomeViewController : UIViewController {
IBOutlet UITableView *table;
TableController *tableController;
}
@end
and
//HomeViewController.m
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
tableController = [[TableController alloc] init];
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
[tableController loadTable]; // HERE I CALL LOADTABLE FROM TABLECONTROLLER CLASS TO PARSE DATA AND POPULATE UITABLEVIEW
[table reloadData];
}
and
// TableController.h
#import <UIKit/UIKit.h>
@interface TableController : NSObject <UITableViewDelegate, UITableViewDataSource> {
UITableView *tableSource;
// a lot of NSMutableArray to parse my data
}
- (void)loadTable;
@property (nonatomic, strong) UITableView *tableSource;
@end
and
//TableController.m
#import "TableController.h"
#import "AFNetworking.h"
@interface TableController ()
@end
@implementation TableController
@synthesize tableSource;
- (void)loadTable {
NSURL *parseURL = // remote URL to parse Data
NSURLRequest *request = [NSURLRequest requestWithURL:parseURL];
AFJSONRequestOperation *parseOperation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// code to parse Data and NSLog to test operation
[tableSource reloadData];
[tableSource setUserInteractionEnabled:YES];
[tableSource scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", [error userInfo]);
}];
[parseOperation start];
[tableSource setUserInteractionEnabled:NO];
}
and, obviously, still in TableController.m, all the classic UITableView delegate methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// my code
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// my code
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// my code
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// my code
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// my code
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// my code
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// my code
}
Well, the parse is perfect (I can test it with a NSLog), but my table is empty. Can u help me?
EDIT: In my code loadTable
parsing method is asynchronous, so table load with right datasource and delegate but BEFORE all data is parsed; in fact IF I SET a fixed numberOfRows and then SCROLL TABLE I can see all the rows populated. But, when I load HomeViewController, *table is still EMPTY.