Search code examples
swiftuicollectionviewjtapplecalendar

How to make 2 calendars in one ViewController using JTAppleCalendar


I have a view controller that contains 2 collection views. So data source and delegate for the first calendar are in view controller extension. For the second i'm trying to make class that conforms data source and delegate protocols. And in ViewDidLoad it gives me an error 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
    }
}

In viewDidLoad

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

Solution

  • Just needed to make an object

    let dataSource = ExpandableCalendarDataSource()
    
    override func ViewDidLoad() {
    super.viewDidLoad()
    
    expandableCalendarCollectionView.calendarDataSource = dataSource
    expandableCalendarCollectionView.ibCalendarDelegate = dataSource
    }