Ios 在swift中使用水平滚动视图和表格视图

Ios 在swift中使用水平滚动视图和表格视图,ios,swift,scrollview,Ios,Swift,Scrollview,因此,我正在与一名设计师合作创建下面的待办事项应用程序,除了在每个页面上显示数据之外,我已经获得了一切 我的问题是,我应该如何创建一个水平滚动视图,其中可以有多个表视图?我尝试过使用xib,这是来自的方法,但是我不能将TableView放在视图中。我也尝试过使用PageController,但是我无法检测用户滚动的方式,这是我更改用户的日期的一部分。今天我尝试将ViewController与scrollview一起使用,但是我无法编辑ViewController上的任何标签。我将把我目前拥有的

因此,我正在与一名设计师合作创建下面的待办事项应用程序,除了在每个页面上显示数据之外,我已经获得了一切

我的问题是,我应该如何创建一个水平滚动视图,其中可以有多个表视图?我尝试过使用xib,这是来自的方法,但是我不能将TableView放在视图中。我也尝试过使用PageController,但是我无法检测用户滚动的方式,这是我更改用户的日期的一部分。今天我尝试将ViewController与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)!
    }
}
类ViewController:UIViewController、UIScrollViewDelegate{
@iBCROLLVIEW:UIScrollView!
让defaults=UserDefaults.standard
日期:日期!
重写func viewDidLoad(){
super.viewDidLoad()
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
让firstPage=storyboard.InstanceEviewController(带有标识符:“FirstPageViewController”)
让secondPage=storyboard.InstanceEviewController(标识符为“SecondPageViewController”)
设thirdPage=storyboard.InstanceEviewController(标识符为:“ThirdPageViewController”)
让ViewController=[第一页、第二页、第三页]
变量idx:Int=0
对于viewController中的viewController{
addChildViewController(viewController);
scrollView.frame=CGRect(x:0,y:0,宽度:view.frame.width,高度:scrollView.frame.height)
scrollView.contentSize=CGSize(宽度:view.frame.width*CGFloat(ViewController.count),高度: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
如果defaults.object(forKey:“date”)==nil{
日期=日期()
}否则{
日期=默认值。对象(forKey:“date”)为!date
}
设decoded=defaults.object(forKey:“data”)为!data
让decodedata=NSKeyedUnarchiver.unarchiveObject(带:decoded)作为![ToDo]
默认设置(日期,forKey:“日期”)
让dateFormatterDate=DateFormatter()
dateFormatterDate.dateFormat=“LLLL dd”
让dateFormatterDay=DateFormatter()
dateFormatterDay.dateFormat=“EEEE”
//self.daylab.text=(dateFormatterDay.string(from:date))
//slide1.day.text=(dateFormatterDay.string(from:date))
}
func ScrollViewDiEndDecelling(scrollView:UIScrollView){
让dateFormatterDate=DateFormatter()
dateFormatterDate.dateFormat=“LLLL dd”
让dateFormatterDay=DateFormatter()
dateFormatterDay.dateFormat=“EEEE”
var x=scrollView.contentOffset.x
如果x>=view.frame.width*2{
scrollView.contentOffset.x=view.frame.width
x=scrollView.contentOffset.x
日期=日期.天(值:1)
}否则,如果x日期{
返回Calendar.current.date(通过添加:.day,value:value,to:noon)!
}
中午:日期{
返回日历。当前。日期(通过设置小时:12,分钟:0,秒:0,of:self)!
}
}

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

我曾经使用过,我也可以向您展示一种手动方式,但这更容易实现。我曾经使用过这个n一个包含三个VC的解决方案,其中中间一个是动态表视图。根据您的要求,您是否使用集合视图查看过(正如您所说,您希望将多个表视图放在水平滚动视图中)。