在xib视图中创建JTAppleCalendar



我试图在xib视图中使用JTAppleCalendar创建一个日历,因为我想要一个分页类型的东西。第一页是一些信息,第二页是日历。我在多个网站上学习了JTAppleCalendar的教程,但我的项目总是崩溃,可能是因为我做错了什么。

我有3个文件,一个用于设置和显示页面的DetailViewController,一个连接xib-vars的dataViewController,以及一个用于日历单元格的CollectionViewCell。

DetailViewController

import UIKit
import JTAppleCalendar
class DetailViewController: UIViewController {
var sportName: String = ""
var numClasses: Int = 0
var pages : [dataViewController]{
get {
let page1: dataViewController = Bundle.main.loadNibNamed("dataView", owner: self, options: nil)?.first as! dataViewController
page1.label.text = sportName
page1.classDuration.text = String(numClasses)
let page2: dataViewController = Bundle.main.loadNibNamed("secondDataView", owner: self, options: nil)?.first as! dataViewController
page2.calendar.calendarDataSource = self as? JTACMonthViewDataSource
page2.calendar.calendarDelegate = self as? JTACMonthViewDelegate
page2.calendar.reloadData()

return [page1, page2]
}
}
@IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!

func configureView() {
// Update the user interface for the detail item.
if let detail = detailItem {
if let label = detailDescriptionLabel {
label.text = detail.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let back = UIBarButtonItem(title: "Back",style: .plain,target: self,action: #selector(backButton(_:)))
self.navigationItem.leftBarButtonItem = back
configureView()
view.bringSubviewToFront(pageControl)
setupScrollView(pages: pages)
pageControl.numberOfPages = pages.count
pageControl.currentPage = 0

}
var detailItem: String? {
didSet {
// Update the view.
configureView()
}
}
@objc
func backButton (_ sender: Any) {
performSegue(withIdentifier: "detailToList", sender: self)
}
func setupScrollView(pages: [dataViewController]){
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(pages.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0 ..< pages.count {
pages[i].frame = CGRect(x: view.frame.width * (CGFloat(i)), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(pages[i])
}
}

}
extension DetailViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
extension dataViewController: JTACMonthViewDataSource, JTACMonthViewDelegate {
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
let startDate = formatter.date(from: "2020 01 01")!
let endDate = Date()
return ConfigurationParameters(startDate: startDate, endDate: endDate)
}
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "dateCell", for: indexPath) as! CollectionViewCell
cell.dateLabel.text = cellState.text
return cell
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! CollectionViewCell
cell.dateLabel.text = cellState.text
}
}

dataViewController

import JTAppleCalendar
class dataViewController: UIView {
//Page 1
@IBOutlet weak var label: UILabel!
@IBOutlet weak var classDuration: UILabel!
//Page 2
@IBOutlet weak var calendar: JTACMonthView!
}

CollectionViewCell

import UIKit
import JTAppleCalendar
class CollectionViewCell: JTACDayCell {
@IBOutlet weak var dateLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}

当然还有xib文件。在DetailViewController中,我将最后一个扩展部分移动到dataViewController,但它没有改变任何内容。我也很确定我做了很多错误或效率低下的事情。

有人能帮助在xib视图中成功实现JTAppleCalendar吗?我们将不胜感激!谢谢

在DetailViewController中,您正在将日历的delegate和dataSource设置为self,这不符合这些协议。

page2.calendar.calendarDataSource = self as? JTACMonthViewDataSource
page2.calendar.calendarDelegate = self as? JTACMonthViewDelegate

如果这些需要在DetailViewController中处理,我建议您从DetailViewController中实现方法开始,而不是在dataViewController中实现方法

extension DetailViewController: JTACMonthViewDataSource, JTACMonthViewDelegate

相关内容

  • 没有找到相关文章

最新更新