ios - 未调用 UIPageViewController 的数据源方法

标签 ios swift uitableview uipageviewcontroller

我的 iOS 应用程序中有以下代码:

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
}
}

我在 TableView 中实例化了这个单元格,但是这个单元格中的页面 View Controller 始终为空,并且没有调用 pageViewController 的数据源方法。你知道为什么他们不被调用吗?

最佳答案

您应该使用记录在案的初始化程序来实例化 UIPageViewController:

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

您在 awakeFromNib 末尾创建的 ViewController 也可以立即放入 pageViewController 中。

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

关于ios - 未调用 UIPageViewController 的数据源方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323096/

相关文章:

ios - 更正 UITableViewHeaderFooterView 的子类化和自动布局

objective-c - 在 iOS 设备上的应用程序之间共享文件

php - 将 JSON 字符串从 iOS 应用程序传递到 PHP Webservice

ios - 如何在 swift 5 中检查多个字符串不为零且不为空?

swift - Alamofire 3.0 请求

iphone - 当用户单击一行时,如何移动到另一个 View Controller ?

ios - Gcm iOS,订阅主题,错误码3004

ios - 如何优化 UIViewController 很长的导航堆栈的内存

ios - Swift - 以触觉、流畅的方式为按钮设置动画的最佳方式?向下滑动菜单按钮?

ios - 如何关闭 UI 导航转换 View clipToBounds 值?