Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 切换到其他日历视图时如何创建新的collectionView_Swift_Xcode - Fatal编程技术网

Swift 切换到其他日历视图时如何创建新的collectionView

Swift 切换到其他日历视图时如何创建新的collectionView,swift,xcode,Swift,Xcode,我在尝试创建新集合视图时遇到问题。我希望有效地拥有一个具有唯一属性的collectionView,然后当我在视图控制器中单击某个日期时,它将变为一个有效的空白collectionView模板,这样用户就可以将其放入其中 如何更改collectionView(当用户单击日期时)、更改为collectionView模板,然后在用户输入数据时保存 以下是控制器的代码以及图片示例: import UIKit import GCCalendar class CalendarViewController

我在尝试创建新集合视图时遇到问题。我希望有效地拥有一个具有唯一属性的collectionView,然后当我在视图控制器中单击某个日期时,它将变为一个有效的空白collectionView模板,这样用户就可以将其放入其中

如何更改collectionView(当用户单击日期时)、更改为collectionView模板,然后在用户输入数据时保存

以下是控制器的代码以及图片示例:

import UIKit
import GCCalendar


class CalendarViewController123: UIViewController, UICollectionViewDelegate {

    // MARK: Properties

    fileprivate var calendarView: GCCalendarView!

    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var datelabel: UILabel!
    @IBOutlet weak var open: UIBarButtonItem!

    @IBAction func trytoday(_ sender: Any) {

     self.calendarView.select(date: Date())
    }
    }

// MARK: - View

extension CalendarViewController123 {

    override func viewDidLoad() {

        super.viewDidLoad()

        open.target = revealViewController()
        open.action = #selector(SWRevealViewController.revealToggle(_:))

        self.addToolbar()
        self.addCalendarView()
        self.addConstraints()

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()

    }
}

// MARK: - Toolbar

extension CalendarViewController123 {

    fileprivate func addToolbar() {

        self.navigationController!.isToolbarHidden = false

        let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let displayMode = UIBarButtonItem(title: "Display Mode", style: .plain, target: self, action: #selector(self.displayMode))

        self.toolbarItems = [ space, displayMode]
    }


    @objc func displayMode() {

        self.calendarView.displayMode = (self.calendarView.displayMode == .month) ? .week : .month
    }

}


// MARK: - Calendar View

fileprivate extension CalendarViewController123 {

    func addCalendarView() {

        self.calendarView = GCCalendarView()

        self.calendarView.delegate = self
        self.calendarView.displayMode = .week


        self.calendarView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(self.calendarView)
    }
}

// MARK: - Constraints

fileprivate extension CalendarViewController123 {

    func addConstraints() {

        self.calendarView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 2).isActive = true
        self.calendarView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        self.calendarView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        self.calendarView.heightAnchor.constraint(equalToConstant: 325).isActive = true
    }
}

// MARK: - GCCalendarViewDelegate

extension CalendarViewController123: GCCalendarViewDelegate {

    func calendarView(_ calendarView: GCCalendarView, didSelectDate date: Date, inCalendar calendar: Calendar) {

        let dateFormatter = DateFormatter()

        dateFormatter.calendar = calendar
        dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMM dd", options: 0, locale: calendar.locale)
        //yyyy
      //  self.navigationItem.title = dateFormatter.string(from: date)

self.datelabel.text = dateFormatter.string(from: date)
    }
}


绿色是视图控制器上的collectionView,它取决于显示的数据以及检索数据的方式。如果你能提供更多的信息,我会相应地更新我的答案

1。选择日期

@IBAction func trytoday(_ sender: Any) {
    self.calendarView.select(date: Date())
    self.fetchData(forDate: selectedDate)
}
2。获取数据

func fetchData(forDate date: Date) {
    //Fetch data and return array of objects
    self.objects = retreivedObjects
    self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return objects.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? Cell else { return UICollectionViewCell}
    cell.configure(withObject: objects[indexPath.item])
    return cell
}
3。加载数据

func fetchData(forDate date: Date) {
    //Fetch data and return array of objects
    self.objects = retreivedObjects
    self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return objects.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? Cell else { return UICollectionViewCell}
    cell.configure(withObject: objects[indexPath.item])
    return cell
}
4。使用数据设置单元格

func fetchData(forDate date: Date) {
    //Fetch data and return array of objects
    self.objects = retreivedObjects
    self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return objects.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? Cell else { return UICollectionViewCell}
    cell.configure(withObject: objects[indexPath.item])
    return cell
}

我在这里非常感谢你回来,本质上是有一个加号按钮,允许用户去另一个控制器选择一个配方,然后当用户返回时,它将与配方的集合视图一起更新。我只是想找出如何选择不同的日期,将有一个空白的收集视图,以便用户可以把食谱在特定的一天