Tableview重新加载不适用于父VC /子VC场景

时间:2013-09-22 00:33:19

标签: uitableview

我有一个场景,我的tableview重新加载不起作用。如果我正确地设计了我的设计或者我需要更改它,请告诉我。

  • 我有一个核心视图控制器,每个标签栏视图控制器都继承自此核心视图控制器
  • 每个标签栏视图控制器都有一个tableview,并实现UITableViewDelegate和UITableViewDataSource协议。
  • 如果应用程序是第一次启动,在核心视图控制器的viewDidLoad中,我有一个从Web获取数据的方法。在子视图控制器的viewDidLoad方法中,我有一个方法来填充tableview的数据源。
  • 所以问题出在第一次启动时,数据被提取并成功保存在Core数据中,但它没有重新加载tableview,所以它是空的。

所以目前按照下面的代码,在第一次启动时,我看到了Alert视图,然后是加载视图,我看到数据被保存在Core Data中。但是第一个标签栏的表视图加载为空,没有数据,原因是,它的[重新加载没有被调用]。但是在下次发布时,数据就在那里。

以下是代码

Core View COntroller


- (void)viewDidLoad
 {
      [super viewDidLoad];
      if (!self.myManagedObjectContext) {

    //Get Shared Instance of managedObjectContext

    self.myManagedObjectContext = [LTDataModel sharedInstance].mainObjectContext;

    //Check if managed object context has required data

    ......    
    if (<data not found> {
        [self fetchDataIntoContext];
    }
}


 -(void)fetchDataIntoContext {
       UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Initializing..."
                                                  message:@"This is your first launch of App"
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
      [message show];
      UIView *loadingView = [[UILoadingView alloc] initWithFrame:self.view.bounds];
      [self.view addSubview:loadingView];
      dispatch_queue_t fetchEventQ = dispatch_queue_create("Fetcher", NULL);
      dispatch_async(fetchEventQ, ^{
           <add data to core data>
        [[LTDataModel sharedInstance] save];
      dispatch_async(dispatch_get_main_queue(), ^{
            [[self.view.subviews lastObject] removeFromSuperview];
      });
  });
}

子视图控制器


 --(void)setMyArrayOfEvents:(NSMutableArray *)myArrayOfEvents {
     if (_arrayOfMyEvents != arrayOfMyEvents) {
       _arrayOfMyEvents = arrayOfMyEvents;
        [self.eventTableView reloadData];
     }
  }

 -(void) viewDidLoad {  
     [super viewDidLoad];

   //Get Shared Instance of managedObjectContext if it does not exist
    if (!self.myManagedObjectContext) {
        self.myManagedObjectContext = [LTDataModel sharedInstance].mainObjectContext;
     }
    //Populate the array which feeds the tableview
    [self populateTableViewArrayFromContext];
  }

  -(void)populateTableViewArrayFromContext
  {
      <Fetch data dfrom Core Data ......>

      self.myArrayOfEvents = [[NSMutableArray alloc] initWithArray:localArray];
  }

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题不在于Child VC的viewDidLoad没有被调用。在后台获取数据完成之前,将删除加载视图。

因此解决方案是我添加了使用空体填充核心(父)视图控制器中的数据源数组的函数。 Child VC实现了该功能。并在删除加载视图之前调用该方法。看下面我在coew view controller中更改的代码

核心视图控制器


 -(void)fetchDataIntoContext {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Initializing..."
                                              message:@"This is your first launch of App"
                                             delegate:nil
                                    cancelButtonTitle:@"OK"
                                    otherButtonTitles:nil];
     [message show];
     UIView *loadingView = [[UILoadingView alloc] initWithFrame:self.view.bounds];
     [self.view addSubview:loadingView];
     dispatch_queue_t fetchEventQ = dispatch_queue_create("Fetcher", NULL);
     dispatch_async(fetchEventQ, ^{
          <add data to core data>
        [[LTDataModel sharedInstance] save];
     dispatch_async(dispatch_get_main_queue(), ^{
          **[self populateTableViewArrayFromContext];**
         [[self.view.subviews lastObject] removeFromSuperview];
     });
  });
}
   -(void)populateTableViewArrayFromContext {
        //empty the implemetation is in Child VC
    }  
相关问题