Currently I have a simple calendar app using FSCalendar
and I want to display an event for every day that matches an array of dates. When the view is loaded it shows the events properly based on what is in the array. After I add a new date, I verify that the array added the new date by setting a breakpoint and in the end I call the calendar.reloadData()
function.
class ViewController: UIViewController {
let calendarA = FSCalendar()
var calendarDaysEvent: [String] = []
// Result with the events of the selected day
var dayEvents: Results<EventsCompleted>?
override func viewDidLoad() {
super.viewDidLoad()
//.../
calendarA.dataSource = self
calendarA.delegate = self
self.calendarA.reloadData()
loadEventsCompleted()
loadEventsPerDay()
}
@IBAction func addItem(_ sender: UIButton) {
// Adds new item to the database
}
func randomHexColor() -> String {
// get the hex color
}
func hexStringToUIColor (hex:String) -> UIColor {
// hexColor to UI
}
func loadEventsCompleted() {
dayEvents = realm.objects(EventsCompleted.self)
eventsTableView.reloadData()
}
func loadEventsPerDay(){
// calendarDays
let id = dayEvents?.count ?? 0
if id != 0 {
print("ID: \(id)")
for x in 1...id {
calendarDaysEvent.append(dayEvents![x-1].date)
}
calendarA.reloadData() // Although I call it here, it is not called
// All the database functions for those things work normally
} else {
print("No events on the calendar")
}
}
}
The numberOfEventsFor
function is called ONLY when the onViewDidLoad function is called.
extension ViewController: FSCalendarDelegate, FSCalendarDataSource {
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
print("Selected \(date)") //2020-01-15 23:00:00 +0000
print("Formatted date: \(date.toDateString(dateFormat: "yyyy-MM-dd"))")
}
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
if calendarDaysEvent.count != 0 {
if calendarDaysEvent.contains(date.toDateString(dateFormat: "yyyy-MM-dd")) {
return 1
} else {
return 0
}
} else {
return 0
}
}
}
When I set a breakpoint on the only return 1
case of the numberOfEventsFor
function, it never triggers. I tried to find how reloadData works and it looks like it recreates the view, meaning that it should reload the events also. I am not sure if I do something wrong calling the reloadData function, coming from an extension.
I removed some part of the code that I believe it is not relevant, but in case you believe it can help giving further context I can add it here.
So this was a stupid issue. The reason that the update of the calendar state didn't work is because I didn't create an outlet to the calendar.