我有一个带有静态单元格的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
我最终从我的SubView
向我的MainView
发送了触发[subViewTable reloadData]
的AFJSONRequestOperation
的success
回调的通知。
SubViewTableController.m AFJSONRequestOperation success 回调:
{
[[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];
}
尝试在后台线程中获取...
-(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];
});
});
}