1

我有一个带有静态单元格的 UITableView,其中包含另一个 UITableView,当我调用reloadData. 使用ios5。

当我运行下面的代码时,subViewTable会显示在viewDidLoadAFNetwork 请求之后定义的值并且不会更新。

根据 NSLog 输出,在我调用reloadDataAFNetwork 请求的成功块后,未调用 cellForRowAtIndexPath。

我的代码如下

主视图控制器.h

#import <UIKit/UIKit.h>
#import "SubViewTableViewController.h"

@interface MainController : UITableViewController{
  SubviewTableViewController *subViewTVC;
  IBOutlet UITableView *subViewTable;
}

主视图控制器.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

子视图表控制器.h

#import <UIKit/UIKit.h>

@interface SubViewTableController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
  NSMutableArray *data;
}

@end

子视图表控制器.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
4

2 回答 2

2

我最终从触发的回调SubView向我发送了一个通知。MainViewsuccessAFJSONRequestOperation[subViewTable reloadData]

SubViewTableController.m AFJSONRequestOperation 成功回调:

{
[[NSNotificationCenter defaultCenter] postNotificationName:@"SubViewUpdated" object:nil];
}

主视图控制器.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];
}
于 2012-12-24T02:22:51.397 回答
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];
        }); 
    });
}
于 2012-12-16T23:31:01.960 回答