如何使用JTAppleCalendar在一个ViewController中制作2个日历

时间:2019-04-20 18:59:20

标签: swift uicollectionview jtapplecalendar

我有一个包含2个集合视图的视图控制器。因此,第一个日历的数据源和委托位于视图控制器扩展中。对于第二个,我正在尝试使类符合数据源和委托协议。在ViewDidLoad中,它给了我一个错误Cannot assign value of type 'ExpandableCalendarDataSource.Type' to type 'JTAppleCalendarViewDataSource?'

import UIKit
import JTAppleCalendar

class ExpandableCalendarDataSource: NSObject, JTAppleCalendarViewDataSource {

    let todayDate = Date()
    let formatter = DateFormatter()

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        self.formatter.dateFormat = "yyyy-MM-dd"
        let startDate = formatter.date(from: "2018 01 01")!
        let endDate = formatter.date(from: "2028 01 01")!
        let numberOfRows = 6
        let firstDayOfAWeek:DaysOfWeek = .monday
        calendar.scrollingMode = .stopAtEachSection
        return ConfigurationParameters(startDate: startDate, endDate: endDate, numberOfRows: numberOfRows, firstDayOfWeek: firstDayOfAWeek)
    }


}

extension ExpandableCalendarDataSource: JTAppleCalendarViewDelegate {
    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {

        let cell = cell as! expandableCalendarDateCell
        cell.dateLabel.text = cellState.text
    }

    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "dateCell", for: indexPath) as! expandableCalendarDateCell
        self.formatter.dateFormat = "yyyy-MM-dd"
        self.formatter.locale = Calendar.current.locale
        self.formatter.timeZone = Calendar.current.timeZone
        let calendarDate = formatter.string(from: cellState.date)

        return cell
    }
}

viewDidLoad

expandableCalendarCollectionView.calendarDataSource = ExpandableCalendarDataSource.self
expandableCalendarCollectionView.calendarDataSource = ExpandableCalendarDataSource.self

1 个答案:

答案 0 :(得分:0)

只需要制作一个物体

let dataSource = ExpandableCalendarDataSource()

override func ViewDidLoad() {
super.viewDidLoad()

expandableCalendarCollectionView.calendarDataSource = dataSource
expandableCalendarCollectionView.ibCalendarDelegate = dataSource
}