Reputation: 173
I am using FSCalendar to render a calendar in my app. At first I used fillDefaultColorFor
to display the events in color like so:
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let dateString2 = self.dateFormatter2.string(from: date)
//set dates with events to yellow
if self.datesArray.contains(dateString2){
return UIColor.yellow
}
return nil
}
But now I want to display events using dots instead using this:
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
let dateString2 = self.dateFormatter2.string(from: date)
print("Dates array at numberOfEvents: ", datesArray)
print("DateString2: ", dateString2)
//render dots if there is an event on that day
if self.datesArray.contains(dateString2){
return 1
}else{
return 0
}
}
However the code above does not render any dots in the calendar. Is there something missing in the numberOfEvents
method?
EDIT: To test numberOfEvents
I added an array of dates like so:
var testDates = ["2018-09-08", "2018-09-16", "2018-09-20", "2018-09-28"]
So the numberOfEvents
looks like this:
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
let dateString = self.dateFormatter2.string(from: date)
print("datesArray: ", datesArray)// this is in "yyyy-MM-dd" format
print("Date String: ", dateString)// displays date in "yyyy-MM-dd" format
print("testDates: ", testDates)
if self.datesArray.contains(dateString){
return 1
}
if testDates.contains(dateString){
return 3
}
return 0
}
When I run the app there are still no event dots being shown in the calendar. Is there something missing in this code? Some function I missed or implemented incorrectly?
Upvotes: 2
Views: 4355
Reputation: 51
You have to include FSCalendarDelegateAppearance method to show event dots colors and FSCalendarDataSource method to give the number of events.
Here's the code
For FSCalendarDelegateAppearance
extension YourController : FSCalendarDelegateAppearance {
// MARK: - FSCalendarDelegateAppearance
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
let dateString = date.toString(dateFormat: "yyyy-MM-dd")
if self.yourDateArr.contains(dateString!) {
return [UIColor.blue]
}
return [UIColor.white]
}
}
For FSCalendarDataSource
extension YourController : FSCalendarDataSource {
// MARK: - FSCalendarDataSource
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
let dateString = date.toString(dateFormat: "yyyy-MM-dd")
if self.yourDateArr.contains(dateString!) {
return 1
}
return 0
}
}
Hope it helps !!
Upvotes: 3
Reputation: 173
I managed to solve my problem with this code:
func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at monthPosition: FSCalendarMonthPosition) {
let dateFormatter3 = DateFormatter()
dateFormatter3.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter3.string(from: date)
//display events as dots
cell.eventIndicator.isHidden = false
cell.eventIndicator.color = UIColor.blue
if self.datesArray.contains(dateString){
cell.eventIndicator.numberOfEvents = 1
}
}
This should display event dots if there are events on a particular date
Upvotes: 2
Reputation: 188
Please check your datesArray value also dateArray and date format must be same.
Whichever date event you want to show that date must be in datesArray and also check matching dates strings are in same formate (yyyy-MM-dd must be).
Upvotes: 0