我有一个名为ViewController
的主要类,它持有UIScrollView
并位于此ViewController
内,我定义了其他三个控制器,其中包含UITableViews,如下所示:
let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1
let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
let viewController3 = storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3
然后将它们添加到滚动视图中。
我已经实施了UIScrollViewDelegate
,因此在完成滚动后,请刷新UITableView
中的ViewController
。
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / self.view.frame.width
}
我收到了页码,但如果我在ViewController2
UITableView
,我该如何重新加载?
答案 0 :(得分:1)
在ViewController
:
var viewControllers = [UIViewController]()
然后设置它们并将它们添加到这个数组中:
viewControllers.append(storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1)
viewControllers.append(storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2)
viewControllers.append(storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
然后您可以在滚动视图委托方法中引用它们:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = Int(floor(scrollView.contentOffset.x / self.view.frame.width))
if page < viewControllers.count {
let viewController = viewControllers[page]
// do whatever with viewController
}
}