Jtapplecalendar: Using outside headers?

Created on 12 Mar 2017  路  9Comments  路  Source: patchthecode/JTAppleCalendar

Hi, i've read the tutorials to generate outside headers, but i'm still struggling to implement it. I want the header to update to the current month as the use scrolls but obviously i don't want the affect of the header scrolling with it. Do i need to design a header for each month?

Most helpful comment

@f15handch1p5 probably you want to put that code in the calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) delegate function.

In my implementation I do something along the lines:

    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM yyyy"
        monthLabel.text = dateFormatter.string(from: visibleDates.monthDates.first!)
    }

I do not know if it is the best way to proceed, but it seems it works ok in my case. I'm sure @patchthecode will correct me if it is too bad :-)

Hope this helps.

All 9 comments

No, you one need to design one header, and update the information as needed.
If is exactly like using a UITableview.

Get the header and configure it to your liking. If you still need help then let me know.

Thanks for the quick reply. I'm still struggling to implement the behaviour however. This is what i have implemented in willDisplaySectionHeader -

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

        let calendar = Calendar(identifier: .gregorian)
        let startDate = range.start
        let month = calendar.dateComponents([.month], from: startDate).month!
        let monthName = DateFormatter().monthSymbols[(month-1) % 12]
        let year = calendar.component(.year, from: startDate)
        let headerCell = header as? MonthSectionHeader
        headerCell?.title.text = monthName + " " + String(year)

        headerCell?.goForwardButton.addTarget(self, action: #selector(CalendarViewController.forwardButtonTapped(sender:)), for: .touchUpInside)
        headerCell?.goBackButton.addTarget(self, action: #selector(CalendarViewController.backButtonTapped(sender:)), for: .touchUpInside)
    }

I don't wont the header to scroll as i scroll the calendar i only want the month to update and that's all. Thanks

The header will only scroll if you implemented header functions.
If you do not want your header to scroll, then please do not implement any header functions.

Simply create label for your header

So you will now update your code above to say:
MYLABEL.title.text = monthName + " " + String(year)

@f15handch1p5 probably you want to put that code in the calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) delegate function.

In my implementation I do something along the lines:

    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM yyyy"
        monthLabel.text = dateFormatter.string(from: visibleDates.monthDates.first!)
    }

I do not know if it is the best way to proceed, but it seems it works ok in my case. I'm sure @patchthecode will correct me if it is too bad :-)

Hope this helps.

@boborbt is correct. This is exactly the place that you put it.
I missed that he was putting it in the willDisplay function

closing issue as it appears to have been resolved.

@patchthecode @boborbt Thanks you fellas!!! That was an amazing help. I was stuck for about 3 days.

@f15handch1p5 probably you want to put that code in the calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) delegate function.

In my implementation I do something along the lines:

    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM yyyy"
        monthLabel.text = dateFormatter.string(from: visibleDates.monthDates.first!)
    }

I do not know if it is the best way to proceed, but it seems it works ok in my case. I'm sure @patchthecode will correct me if it is too bad :-)

Hope this helps.

It doesn't work on first load until we scroll it back and forth. Any solution for this? @patchthecode

@ekchhuon i am not exactly sure what you are trying to do.

Was this page helpful?
0 / 5 - 0 ratings