Ios 如何在UITableViewCell中实现UIPageViewController

Ios 如何在UITableViewCell中实现UIPageViewController,ios,swift,Ios,Swift,我想在“UITableViewCell”内制作幻灯片。所以,我需要使用“UIPageViewController”来制作幻灯片。我尝试在此处实现解决方案,但无法将UIPageViewController添加到UITableViewCell内容视图 这是我在UITableViewCell中的代码 let slideshowPageViewController = SlideshowPageViewController() self.addSubview(slideshowPageViewContr

我想在“UITableViewCell”内制作幻灯片。所以,我需要使用“UIPageViewController”来制作幻灯片。我尝试在此处实现解决方案,但无法将UIPageViewController添加到UITableViewCell内容视图

这是我在UITableViewCell中的代码

let slideshowPageViewController = SlideshowPageViewController()
self.addSubview(slideshowPageViewController)
错误:无法将“UIPageViewController”类型的值转换为 应为参数类型“UIView”


有人能建议其他解决方案吗?

我不建议这样做。UIPageViewController是一个UIViewController。我总是发现它对于任何现实世界的使用来说都太复杂了——特别是如果你只是需要在幻灯片中循环浏览图像

我建议在UITableViewCell中添加一个子UIScrollView,并自己处理幻灯片放映代码

在设置UIScrollView时有一些好的做法。最重要的部分是计算scrollViewDidScroll委托调用中的当前索引:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // Calculate current index
    let index =  Int(round(scrollView.contentOffset.x/scrollView.frame.size.width))
    updateCurrentIndex(index: index)
}
如果需要为用户滚动幻灯片,请不要直接调用updateCurrentIndex。相反,调用setContentOffset时,委托仍将激发,并且您的updateCurrentIndex方法将像用户手动滚动一样工作

@IBAction func didNext() {
    if (currentIndex < scrollView.subviews.count-1) {
        let point = CGPoint(x: scrollView.bounds.size.width * CGFloat(currentIndex + 1), y: 0)
        scrollView.setContentOffset(point, animated: true)
    } else {
        performSegue(withIdentifier: "showTermsSegue", sender: self)
    }
}
@IBAction func didNext(){
if(currentIndex
我不建议这样做。UIPageViewController是一个UIViewController。我总是发现它对于任何现实世界的使用来说都太复杂了——特别是如果你只是需要在幻灯片中循环浏览图像

我建议在UITableViewCell中添加一个子UIScrollView,并自己处理幻灯片放映代码

在设置UIScrollView时有一些好的做法。最重要的部分是计算scrollViewDidScroll委托调用中的当前索引:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // Calculate current index
    let index =  Int(round(scrollView.contentOffset.x/scrollView.frame.size.width))
    updateCurrentIndex(index: index)
}
如果需要为用户滚动幻灯片,请不要直接调用updateCurrentIndex。相反,调用setContentOffset时,委托仍将激发,并且您的updateCurrentIndex方法将像用户手动滚动一样工作

@IBAction func didNext() {
    if (currentIndex < scrollView.subviews.count-1) {
        let point = CGPoint(x: scrollView.bounds.size.width * CGFloat(currentIndex + 1), y: 0)
        scrollView.setContentOffset(point, animated: true)
    } else {
        performSegue(withIdentifier: "showTermsSegue", sender: self)
    }
}
@IBAction func didNext(){
if(currentIndex
我努力寻找解决方案。而且,我已经发现了这一点

在rowAtIndexPath中,我添加了childViewController,它对我有效

let slideshowPageViewController = SlideshowPageViewController()
self.addChildViewController(slideshowPageViewController)
cell = tableView.dequeueReusableCell(withIdentifier: "Slideshow", for: indexPath)

cell.contentView.addSubview(slideshowPageViewController.view)

希望这能帮助任何有同样问题的人。

我努力寻找解决方案。而且,我已经发现了这一点

在rowAtIndexPath中,我添加了childViewController,它对我有效

let slideshowPageViewController = SlideshowPageViewController()
self.addChildViewController(slideshowPageViewController)
cell = tableView.dequeueReusableCell(withIdentifier: "Slideshow", for: indexPath)

cell.contentView.addSubview(slideshowPageViewController.view)

希望这能帮助任何有同样问题的人。

我喜欢你的解决方案,但问题是我想制作自行车图像。我可以用无限循环实现UIScrollView吗?您想回到第一页重新开始,还是看起来总是在移动到新页面(从左到右)?您是允许用户交互(允许用户循环图像),还是希望自动循环图像?不管怎样,我不认为UIPageViewController允许无限循环。我想自动循环浏览图像,用户也可以滑动以更改幻灯片图像。(像旋转木马一样)这需要更多的工作,但我仍然会使用UIScrollView。其中有三个(左、中、右)内容视图。当索引更改时,您需要重新定位和重新加载内容视图。我喜欢您的解决方案,但问题是我想制作循环图像。我可以用无限循环实现UIScrollView吗?您想回到第一页重新开始,还是看起来总是在移动到新页面(从左到右)?您是允许用户交互(允许用户循环图像),还是希望自动循环图像?不管怎样,我不认为UIPageViewController允许无限循环。我想自动循环浏览图像,用户也可以滑动以更改幻灯片图像。(像旋转木马一样)这需要更多的工作,但我仍然会使用UIScrollView。其中有三个(左、中、右)内容视图。当索引更改时,您需要重新定位并重新加载内容视图。