Search code examples
iosuitableviewuipageviewcontroller

TableView Empty in UIPageViewController


I need help for implement some UIViewController with TableView in UIPageViewController

This is my current code, but not works. Some UIViewController appear but the TableView is empty, no cells.

APageViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  pages = [NSMutableArray array];

  self.delegate = self;
  self.dataSource = self;
}

- (void)viewDidAppear:(BOOL)animated{
  [super viewDidAppear:animated];

  [self setupPageViewController];
}

- (void)setupPageViewController{

  for (int i = 0; i < items.count; i++) {
    AViewControllerWithTableView *detail = (AViewControllerWithTableView *)[self.storyboard instantiateViewControllerWithIdentifier:@"VCID"];
    [detail reloadTableView:items[i]]; // tableview reload data here
    [pages addObject:detail];
  }

  [self setViewControllers:@[pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger currentIndex = [pages indexOfObject:viewController];
    NSInteger previousIndex = currentIndex-1;
    if (previousIndex < 0) {
        return nil;
    }

    return pages[previousIndex];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSInteger currentIndex = [pages indexOfObject:viewController];
    NSInteger nextIndex = currentIndex+1;
    if (nextIndex > pages.count - 1) {
        return nil;
    }

    return pages[nextIndex];
}

AViewControllerWithTableView

@implementation AViewControllerWithTableView{
    NSMutableArray *details;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    details = [NSMutableArray array];
}

- (void)reloadTableView:(NSDictionary *)detail{

  if([detail.type isEqualToString:@"A"]){
    [details addObject:something];
  } else if([detail.type isEqualToString:@"B"]){
    [details addObject:something];
  } else if([detail.type isEqualToString:@"C"]){
    [details addObject:something];
  } else {
    [details addObject:something];
  }

  [self.detailTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  NSLog(@"details: %@", details); // details always empty / @[]
  return details.count;
}

tableview is empty, no cells.

How you implement this case?


Solution

  • Issue :

    You are setting

    details = [NSMutableArray array];
    

    in the viewDidLoad of your tableView Controller which gets called once PageViewController loads your tableView.

    but you are sending data to your tableView Controller by calling [detail reloadTableView:items[i]]; even before tableView is loaded.

    So either you were calling reloadTableView while details was always nil or even if it was populated your statement details = [NSMutableArray array]; in viewDidLoad now cleared it :)

    Solution :

    for (int i = 0; i < items.count; i++) {
        AViewControllerWithTableView *detail = (AViewControllerWithTableView *)[self.storyboard instantiateViewControllerWithIdentifier:@"VCID"];
        if(detail.details == nil) {
            detail.details = [NSMutableArray array];
        }
        [detail.details.addObject:items[i]];
        [pages addObject:detail];
      }
    

    Finally remove details = [NSMutableArray array]; from viewDidLoad of your tableView