gpt4 book ai didi

swift - 切换到另一个calendarView时如何创建新的collectionView

转载 作者:行者123 更新时间:2023-11-30 10:35:48 28 4
gpt4 key购买 nike

我在尝试创建新的 Collection View 时遇到问题。我想有效地拥有一个具有独特属性的collectionView,然后当我单击 View Controller 中的日期时,它实际上会更改为一个空白的collectionView模板,以便用户可以将其放在食谱中。

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

这是 Controller 的代码以及图片示例:

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)
}
}

enter image description here

绿色是 View Controller 上的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()
}

<强>3。加载数据

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objects.count
}

<强>4。使用数据设置单元格

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
}

关于swift - 切换到另一个calendarView时如何创建新的collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061997/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com