I have a UITableView with static cell that contains another UITableView which is not being updated when I call reloadData. Using ios5.
When I run the code below, the subViewTable displays the values defined in the viewDidLoad and does not update after the AFNetwork request.
Based on the NSLog output, cellForRowAtIndexPath is not being called after I call reloadData in the success block of my AFNetwork request.
My code is below
MainViewController.h
#import <UIKit/UIKit.h>
#import "SubViewTableViewController.h"
@interface MainController : UITableViewController{
SubviewTableViewController *subViewTVC;
IBOutlet UITableView *subViewTable;
}
MainViewController.m
@import "SubViewTableViewController.h"
@implementation MainController
- (void)viewDidLoad
{
[super viewDidLoad];
if (subViewTableViewController == nil)
{
subViewTVC = [[SubViewTableViewController alloc] init];
}
[subViewTable setDataSource: subViewTVC];
[subViewTable setDelegate: subViewTVC];
subViewTableViewController.view = subViewTableViewController.tableView;
}
@end
SubViewTableController.h
#import <UIKit/UIKit.h>
@interface SubViewTableController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *data;
}
@end
SubViewTableController.m
#import "SubViewTableController.h"
#import "AFNetworking.h"
@implementation SubViewTableController
- (void)fetchCustomData
{
request = // Set NSM mutable request;
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Omitted code that adds JSON objects to data array
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {};
[operation start];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (data == nil)
{
data = [NSMutuableArray arrayWithObjects:@"1",@"2", nil];
}
[self fetchCustomData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Data Count: %i", data.count);
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"DataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
NSLog(@"Cell item: %@", [data objectAtIndex:indexPath.row]);
return cell;
}
@end
NSLog Output
2012-12-16 13:59:31.538 testing[37592:f803] Data Count: 2
2012-12-16 13:59:31.539 testing[37592:f803] Cell item: 1
2012-12-16 13:59:31.541 testing[37592:f803] Cell item: 2
2012-12-16 13:59:31.575 testing[37592:f803] Data Count: 6
I ended up sending a notification from my
SubViewto myMainViewfrom thesuccesscallback ofAFJSONRequestOperationwhich triggered[subViewTable reloadData].SubViewTableController.m AFJSONRequestOperation success callback:
MainViewController.m