MilkBottle
MilkBottle

Reputation: 4342

JTAppleCalendar How to set current date and current month

Hi I am newbie to JTAppleCalendar. I need help. I have installed it thru cocoapods and used some of the code as below.

I need help on how to use the basic functions such as:

  1. Set the calendar for the current month

JTC.scrollToDate(Date())

  1. show the current date on the calendar.

  2. Do I need to specify or configure how many years this calendar support like 100 or 200 years?

  3. Is this way correct to get a reference for the JTAppleCalendar

    @IBOutlet weak var JTC: JTAppleCalendarView!

Please help to correct the code.

  import UIKit
    import JTAppleCalendar


    class ViewController: UIViewController {

        let formatter = DateFormatter()

        @IBOutlet weak var JTC: JTAppleCalendarView!


        override func viewDidLoad() {
            super.viewDidLoad()

        let d = Date()        
        var dateArr = [Date]()        
        dateArr.append(d)

     JTC.scrollToDate(Date())


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

    extension ViewController: JTAppleCalendarViewDelegate,JTAppleCalendarViewDataSource{

        func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {

            formatter.dateFormat = "yyyy MM dd"
            formatter.timeZone = Calendar.current.timeZone
            formatter.locale = Calendar.current.locale

            let startDate = formatter.date(from: "2017 01 01")!
            let endDate = formatter.date(from: "2117 12 31")!


            let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)

            return parameters


        }

        func calendar(_ calendar:JTAppleCalendarView,cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell{

            let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell

            cell.dateLabel.text = cellState.text

            return cell            

        }        

    }

Thanks

Upvotes: 1

Views: 5319

Answers (1)

Sean McCue
Sean McCue

Reputation: 31

The issue was solved for me by adding three methods in ViewDidLoad and setting start and end dates as follows:

override func viewDidLoad()  {

    self.calendarView.visibleDates {[unowned self] (visibleDates: DateSegmentInfo) in
        self.setupViewsOfCalendar(from: visibleDates)
    }

    self.calendarView.scrollToDate(Date(),animateScroll: false)
    self.calendarView.selectDates([ Date() ])


}

In Calendar configuration set dates as follows:

func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
    formatter.dateFormat = "yyy MM dd"
    formatter.timeZone = Calendar.current.timeZone
    formatter.locale = Calendar.current.locale

    var dateComponent = DateComponents()
    dateComponent.year = 1
    let startDate = Date()
    let endDate = Calendar.current.date(byAdding: dateComponent, to: startDate)
    let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate!, numberOfRows: 6, calendar: Calendar.current, generateInDates: .forFirstMonthOnly, generateOutDates: .off, firstDayOfWeek: .sunday, hasStrictBoundaries: true)


    return parameters

}

Upvotes: 3

Related Questions