Skip to content

Instantly share code, notes, and snippets.

@philipshen
Last active September 6, 2018 15:47
Show Gist options
  • Save philipshen/575ff1287a5fc372ca3ff396bd884ba6 to your computer and use it in GitHub Desktop.
Save philipshen/575ff1287a5fc372ca3ff396bd884ba6 to your computer and use it in GitHub Desktop.
JTAppleCalendar Issue
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: "2017 12 31")!
let parameters = ConfigurationParameters(
startDate: startDate,
endDate: endDate,
numberOfRows: 6,
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: DaysOfWeek.monday,
hasStrictBoundaries: false
)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "DateCell", for: indexPath) as! CalendarDateCell
configureCell(cell: cell, cellState: cellState)
return cell
}
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
configureCell(cell: cell as! CalendarDateCell, cellState: cellState)
}
func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
setMonthAndYear(date: visibleDates.monthDates.first!.date)
}
func configureCell(cell: CalendarDateCell, cellState: CellState) {
cell.dateLabel.text = cellState.text
cell.isToday = Calendar.current.isDateInToday(cellState.date)
cell.inCurrentMonth = cellState.dateBelongsTo == .thisMonth
}
// The month/year is working fine
func setMonthAndYear(date: Date) {
formatter.dateFormat = "yyyy"
yearLabel.text = formatter.string(from: date)
formatter.dateFormat = "MMMM"
monthLabel.text = formatter.string(from: date)
}
calendarView.calendarDelegate = self
calendarView.calendarDataSource = self
// Dynamically set the "height" constraint
calendarHeight.constant = (CalendarDateCell.cellSize + 4) * 6
// Initial month/year
calendarView.visibleDates { [weak self] visibleDates in
self?.setMonthAndYear(date: visibleDates.monthDates.first!.date)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment