简体   繁体   中英

UiTableView in viewController - delegate and dataSource methods not getting called

I just finished a tutorial about creating a UIPageViewController. The source code of the tutorial is also available in the link at the end of the article. Now instead of the imageView I wanted to add a tableView with custom cells for every page. In the PageContentViewController.h file I added the IBOutlet for the tableView which is called "theTableView".

Now for the viewController I did the following:

viewcontroller.h

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

@interface ViewController : UIViewController <UIPageViewControllerDataSource,UITableViewDataSource,UITableViewDelegate>
...
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *pageTitles;
@property (strong, nonatomic) NSArray *tableViews;


@end

viewController.m

- (void)viewDidLoad
{
    //some code
    UITableView* v1=[[UITableView alloc] init];
    v1.dataSource=self;
    v1.delegate=self;

    UITableView* v2=[[UITableView alloc] init];
    v2.dataSource=self;
    v2.delegate=self;

    _tableViews= @[v1, v2];

    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];


 // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    pageContentViewController.theTableView=self.tableViews[0];
    pageContentViewController.thetableView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

 // some code
}

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    //some code

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    pageContentViewController.theTableView = self.tableViews[index];

    return pageContentViewController;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustumCell *cell= (CustumCell *)[tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];

    if (cell==nil) {cell = [[CustumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];}

    return cell;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

The last 3 methods are not getting called. What am I doing wrong here?

Thanks!

EDIT :added the suggestions in the answers below. Still get the same result. When I add SnowMax's solution (eg [self.view addSubview v1]) the methods are getting called, but the tableView does not show in the pageContentViewController because I add the subviews in the viewController. Trying to add the subviews v1 and v2 to the pageContentViewController.view results in not calling the delegate methods again?

EDIT2 I ended up putting the tableView outlet in the pageContentViewController.h file and set the dataSource and the delegate of the tableView there. Now it works great. Still don't know how I could've set the tableView in the viewController and correctly show the tables as pages in the pageContentViewController, but this solution will do for now. I just started learning objectiveC/cocoa so I will figure it out later.

The problem is, that the TableViews are not added to your view. You just initialize the view.

In the tutorial a storyboard is used to place the UI elements. You can go this way and add an UITableView to the storyboard and create a delegate and set the delegate and the datasource in the viewDidLoad method.

ViewController.h

@interface ViewController : UIViewController <UIPageViewControllerDataSource,UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

ViewController.m

- (void)viewDidLoad {
    // Set Delegate and Datasource
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
}

If you want to create the TableView in code you should add it to your view.

ViewController.m

- (void)viewDidLoad {
    // Init
    UITableView* v1=[[UITableView alloc] init];
    v1.dataSource=self;
    v1.delegate=self;

    // Place at your view
    [self.view addSubview:v1];
}

You have to add the tableview and pageViewController to the viewController and set their delegates. in your code, you did not setup the pageViewController

in viewDidLoad , add the following :

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];

PageContentViewController *initialViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.theTableView = self.tableViews[0];

NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];

See here for a complete tutorial

Seems that you did every thing right but, you didn't set the frame of table view. Set the frame of tableview.

if you don't set the frame of tableview then only method getting called is .

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

hope it works for you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM