我有一个带静态单元格的UITableView,其中包含另一个UITableView,当我调用reloadData
时,它没有被更新。使用ios5。
当我运行下面的代码时,subViewTable
会显示viewDidLoad
中定义的值,并且在AFNetwork请求后不会更新。
基于NSLog输出,在我的AFNetwork请求的成功块中调用reloadData
后,没有调用cellForRowAtIndexPath。
我的代码在
下面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输出
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
答案 0 :(得分:2)
我最终从SubView
发送MainView
success
来自AFJSONRequestOperation
的{{1}}回调,其中[subViewTable reloadData]
已发出通知。
SubViewTableController.m AFJSONRequestOperation成功回调:
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"SubViewUpdated" object:nil];
}
<强> MainViewController.m 强>
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subViewUpdated) name:@"SubViewUpdated" object:nil];
}
- (void)subViewUpdated
{
[subViewTable reloadData];
}
- (void)viewDidUnload
{
//Unregister notifications
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
答案 1 :(得分:0)
尝试在后台线程中抓取...
-(void)updateDataInBackground {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {
// Background Thread:
// hard work/updating/fetching here:
// when finished ...
dispatch_async(dispatch_get_main_queue(), ^(void) {
// Main Thread:
[myTableView reloadData];
});
});
}