在Swift

时间:2015-07-13 11:55:11

标签: ios swift uitableview uiscrollview

我有一个名为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,我该如何重新加载?

1 个答案:

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