2016-02-10 52 views
0

我在我的iOS應用程序下面的代碼:UIPageViewController的數據來源方法不叫

class BannerTableViewCell: UITableViewCell, UIPageViewControllerDataSource { 
private var pageViewController: UIPageViewController! 
private var pages: [UIViewController] = [] 

override func awakeFromNib() { 
    super.awakeFromNib() 
    self.backgroundColor = UIColor.clearColor() 
    self.backgroundView?.backgroundColor = UIColor.clearColor() 
    self.contentView.backgroundColor = UIColor.clearColor() 
    self.selectionStyle = UITableViewCellSelectionStyle.None 

    pageViewController = UIPageViewController(); 

    self.pageViewController.dataSource = self 
    pageViewController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130) 
    self.addSubview(pageViewController.view) 

    //Example data 
    let v1 = UIViewController() 
    v1.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130); 
    v1.view.backgroundColor = UIColor.blueColor() 

    let v2 = UIViewController() 
    v2.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130); 
    v2.view.backgroundColor = UIColor.greenColor() 

    pages.append(v1) 
    pages.append(v2) 
} 



func pageViewController(pageViewController: UIPageViewController, 
    viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { 
     guard let viewControllerIndex = pages.indexOf(viewController) else { 
      return nil 
     } 

     let previousIndex = viewControllerIndex - 1 

     guard previousIndex >= 0 else { 
      return nil 
     } 

     guard pages.count > previousIndex else { 
      return nil 
     } 

     return pages[previousIndex] 
} 

func pageViewController(pageViewController: UIPageViewController, 
    viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { 
     guard let viewControllerIndex = pages.indexOf(viewController) else { 
      return nil 
     } 

     let nextIndex = viewControllerIndex + 1 
     let orderedViewControllersCount = pages.count 

     guard orderedViewControllersCount != nextIndex else { 
      return nil 
     } 

     guard orderedViewControllersCount > nextIndex else { 
      return nil 
     } 

     return pages[nextIndex] 
} 

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { 
    return pages.count 
} 

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { 
    return 0 
} 
} 

我實例在表視圖這種細胞,但是頁面視圖控制器此單元格始終是空的,不調用pageViewController的數據源方法。你有什麼想法,爲什麼他們不被稱爲?

回答

0

您應該使用記錄初始化實例化UIPageViewController:

public init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?) 

而且你在awakeFromNib年底創建ViewControllers可以放入pageViewController的時候了。

pageViewController.setViewControllers([v1, v2], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) 
+0

感謝你提到presentationCountForPageViewController和presentationIndexForPageViewController方法構造函數的用法是所謂的,但2個以前的都沒有。此外,當我在實例化的單元格上滑動時,該應用程序崩潰,出現SIGABRT錯誤。 –

+0

那次撞車的細節是什麼? – jervine10

+0

在另一個筆記上,你有一個相當複雜的設置。你在UITableViewCell中放置一個UIPageViewController。根據我的經驗,將UIViewController放在TableViewCells中通常會導致問題。 – jervine10