Initial selected date getting ignored
v8.0.0 (working on checking this in 8.0.2)
Description
Setting a selected date inside viewDidLoad will not be added to the selectedDate array. The use case is to set the current date as preselected but then selecting another date will clear today. However when i set a breakpoint after selectedDates() the array is empty, and then two dates get highlighted.
Steps To Reproduce
- Create a calendar view
- Inside
viewDidLoadassign other settings (delegate, etc) and includeselectDates - When the view loads, click another date in the calendar to clear today's date selection but instead 2 dates are highlighted
Expected Behavior
Selecting the second date in the month should clear the other dates (I have allow only 1 date selected)
just checking, but are you sure you renamed the didSelect and didDeselect to the correct names as shown on the 8.0 migration page?
Yes, I believe I corrected those during the last update
i cannot recreate it on the sample project attached to this repo. Can you paste me relevant code in your
- cellForItem functions
- willDisplayCell functions
I was able to update to 8.0.2 but it is crashing on assigning the datasource so I haven't confirmed if it has the same issue. Here are the functions you're requesting
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarDateCollectionViewCell", for: indexPath) as! CalendarDateCollectionViewCell
cell.render(cellState)
// set the secondary properties
cell.hasEvents = delegate?.calendarDateHasActivities(self, selectedDate: cellState.date) ?? false
cell.needsAttention = delegate?.calendarDateRequiresAttention(self, selectedDate: cellState.date) ?? false
return cell
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! CalendarDateCollectionViewCell
cell.render(cellState)
}
ok, those functions look ok. There are no conflicts there.
Ok the final question I would have is, can i see your viewDidLoad function?
That's going to be a little trickier as my calendar is embedded inside another view on the controller. The container is populated from the NIB and the code below is run inside the setup method.
// calendar
calendarView.calendarDelegate = self
calendarView.calendarDataSource = self
calendarView.register(UINib(nibName: "CalendarDateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CalendarDateCollectionViewCell")
calendarView.scrollingMode = .stopAtEachCalendarFrame
calendarView.scrollDirection = .horizontal
calendarView.scrollToDate(selectedDate, triggerScrollToDateDelegate: false, animateScroll: false, preferredScrollPosition: .top, extraAddedOffset: 0) {
self.calendarView.selectDates([self.selectedDate])
print(self.calendarView.selectedDates.description)
}
calendarView.reloadData()
If i put a breakpoint at the print statement, the calendarView.selectedDates array is empty
will get back to this over the weekend
Im back at this. Kindly create a minimal sample project (just the bare minimum to get this error to happen). I do not need any of your secret company code, so you can create a new empty non designed project. Once it recreates the issue, then all will be ok.
Thank you for taking an interest in this. One of the other developers on our project looked into it more closely and found that if we call selectDates() and scrollToDate() too close together, the animation/scroll has not completed and the selection gets out of sync. He solved it by adding a delay to the date selection after scrolling.
His suggestion was to have a callback in the scrollToDate method that would allow you to set the correct selection on the correct thread.
Hopefully that provides enough info without the entire test project but otherwise the library is working great for us.
Follow up: He provided more info to say that it seems that the issue is related to these three lines together:
calendarView.selectDates([selectedDate])
calendarView.scrollToDate(selectedDate, triggerScrollToDateDelegate: false, animateScroll: false, preferredScrollPosition: .top, extraAddedOffset: 0)
calendarView.reloadData()
where reloadData() specifically causes an issue (meaning remove the call and things work correctly)
this help very much. If will rethink the logic in that section. Call backs makes sense. Tell your dev i said thanks.