快速使用水平滚动视图和表格视图

时间:2018-07-30 21:40:06

标签: ios swift scrollview

因此,我正在与设计师合作,在下面创建待办事项应用程序,除了在每个页面上显示数据之外,我已经拥有所有功能。

https://projects.invisionapp.com/share/4BMYIMOYP9W#/screens

我的问题是,我应该如何创建一个可以在其中包含多个表视图的水平滚动视图?我尝试使用xib(这是this tutorial中的方法),但是无法在视图中放置表格视图。我也尝试过使用PageController,但是由于这种方式,我无法检测到用户滚动的方式,这是更改用户上班日期的一部分。今天,我尝试将ViewControllers与scrollview一起使用,但是我无法在ViewController上编辑任何标签。我将下面的代码放在下面。

ViewController.swift

class ViewController: UIViewController, UIScrollViewDelegate{

@IBOutlet weak var scrollView: UIScrollView!
let defaults = UserDefaults.standard
var date: Date!


override func viewDidLoad() {
    super.viewDidLoad()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let firstPage = storyboard.instantiateViewController(withIdentifier: "FirstPageViewController")
    let secondPage = storyboard.instantiateViewController(withIdentifier: "SecondPageViewController")
    let thirdPage = storyboard.instantiateViewController(withIdentifier: "ThirdPageViewController")
    let viewControllers = [firstPage,secondPage,thirdPage]

    var idx:Int = 0
    for viewController in viewControllers {

        addChildViewController(viewController);
        scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: scrollView.frame.height)
        scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(viewControllers.count), height: scrollView.frame.height)
        viewController.view.frame = CGRect(x: view.frame.width * CGFloat(idx), y: 0, width: view.frame.width, height: scrollView.frame.height);
        scrollView!.addSubview(viewController.view)
        viewController.didMove(toParentViewController: self)
        idx += 1;
    }
    scrollView.contentOffset.x = view.frame.width
    self.scrollView.delegate = self


    if defaults.object(forKey: "date") == nil {
        date = Date()
    }else {
        date = defaults.object(forKey: "date") as! Date
    }


    let decoded  = defaults.object(forKey: "data") as! Data
    let decodedData = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [ToDo]
    defaults.set(date, forKey: "date")

    let dateFormatterDate = DateFormatter()
    dateFormatterDate.dateFormat = "LLLL dd"
    let dateFormatterDay = DateFormatter()
    dateFormatterDay.dateFormat = "EEEE"


    //self.dayLabel.text = (dateFormatterDay.string(from: date))
    //slide1.day.text = (dateFormatterDay.string(from: date))

}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let dateFormatterDate = DateFormatter()
    dateFormatterDate.dateFormat = "LLLL dd"
    let dateFormatterDay = DateFormatter()
    dateFormatterDay.dateFormat = "EEEE"

    var x = scrollView.contentOffset.x
    if x >= view.frame.width * 2 {
        scrollView.contentOffset.x = view.frame.width
        x = scrollView.contentOffset.x
        date = date.days(value: 1)
    }else if x <= 0 {
        scrollView.contentOffset.x = view.frame.width
        x = scrollView.contentOffset.x
        date = date.days(value: -1)
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
extension Date {

    func days(value: Int) -> Date {
        return Calendar.current.date(byAdding: .day, value: value, to: noon)!
    }


    var noon: Date {
        return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
    }
}

老实说,我不知道如何破解它,我要请设计师重新设计UX,但是我真的很喜欢它现在是如何工作的。或者至少,它应该如何工作。抱歉,如果这是一个新手问题,我很快就知道了。

0 个答案:

没有答案