Ios 使用addChildViewController不会调用UITableViewDatasource方法

Ios 使用addChildViewController不会调用UITableViewDatasource方法,ios,uitableview,Ios,Uitableview,使用addChildViewController不会调用UITableViewDatasource方法 没有数据源或委托方法 有办法解决这个问题吗 下面我添加了调用addChildViewController方法的父控制器的代码。 下面,我发布了子viewController代码(没有UIViewTableDatasource) 父代码: self.commentsController = [[CommentsVC alloc] initWithNibName:@"CommentsVC" bu

使用addChildViewController不会调用UITableViewDatasource方法 没有数据源或委托方法 有办法解决这个问题吗

下面我添加了调用addChildViewController方法的父控制器的代码。 下面,我发布了子viewController代码(没有UIViewTableDatasource)

父代码:

 self.commentsController = [[CommentsVC alloc] initWithNibName:@"CommentsVC" bundle:nil restaurant:_resto];

    UIView *newview = [[UIView alloc] initWithFrame:self.viewComments.bounds];


    self.commentsController.view = newview;
    self.commentsController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.viewComments addSubview:self.commentsController.view];

    [self addChildViewController:self.commentsController];
子代码

- (id)initWithRestaurant:(Restaurant *)resto{
self = [super init];
if (self) {
    _resto = resto;
    if(resto.reviews.count == 0){
        dispatch_queue_t myQueue = dispatch_queue_create("q_getReviews", NULL);
        dispatch_async(myQueue, ^{
            id resultReviews = [[RestaurantManager sharedInstance] getRestaurantReviews:resto orderBy:[NSNumber numberWithInt:1]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if([resultReviews isKindOfClass:[NSError class]]){
                    NSError *err = (NSError *)resultReviews;
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:err.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                    [alert show];
                }else{
                    self.resto.reviews =  (NSMutableArray *)resultReviews;
                    [((UITableView *)[self.view viewWithTag:kTblComments]) reloadData];
                }
            });
        });
        dispatch_release(myQueue);
    }
}
return self;
}

  • 在您调用的父代码中,
    [[CommentsVC alloc]initWithNibName:@“CommentsVC”bundle:nil餐厅:_resto]
    用于初始化子视图控制器,但在子视图控制器中,您正在加载我认为用于表视图的数据,因此您的数据源可能为空

  • 现在调用
    [((UITableView*)[self.view view with tag:kTblComments])重新加载数据还为时过早
    在init方法中,表视图尚未初始化

  • 因此,请确保为子视图控制器调用了正确的init方法,并确保表视图数据源不为空,然后可以在初始化表视图后调用
    [yourTableView reloadData]
    ,将其设置为委托并添加为子视图。

    已解决

    我正在分配一个不包含uiTable的新视图

    UIView *newview = [[UIView alloc] initWithFrame:self.viewComments.bounds];
    self.commentsController.view = newview;
    

    只要去掉它,代码就行了

    这并不能解决问题。因为当我通过uiNavigationController push更改addChildViewController的
    UIView *newview = [[UIView alloc] initWithFrame:self.viewComments.bounds];
    self.commentsController.view = newview;