Travis Whitten
Travis Whitten

Reputation: 64

JTAppleCalendar Swift For Loop

I am using the following function to try and print out a Month for each header as you scroll through the calendar. There must be a problem with how I'm doing my for loop as the only month Im getting out is December. Any ideas? Thanks

With the print("(names)"), i am getting all of the months printed out on the console. However, I can't get them printed out correctly visually.

func calendar(_ calendar: JTAppleCalendarView, willDisplaySectionHeader header: JTAppleHeaderView, range: (start: Date, end: Date), identifier: String) {

        //let months: [String] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        let months = Calendar.current.monthSymbols
        let headerCell = (header as? MonthsHeader)
        for names in months {
            print("\(names)")
        headerCell?.monthsHeader.text = names

        }
    }

Upvotes: 1

Views: 725

Answers (1)

Just a coder
Just a coder

Reputation: 16730

Here is how you do it.

func calendar(_ calendar: JTAppleCalendarView, willDisplaySectionHeader header: JTAppleHeaderView, range: (start: Date, end: Date), identifier: String) {
   let startDate = range.start
   let month = myCalendar.dateComponents([.month], from: startDate).month!
   let monthName = DateFormatter().monthSymbols[(month-1) % 12]
   let year = testCalendar.component(.year, from: startDate)
   let headerCell = header as? MyHeaderView
   headerCell?.title.text = monthName + " " + String(year)
}

myCalendar --> this is a Calendar() instance

MyHeaderView-> this is the header class

Upvotes: 3

Related Questions