I have a bit complicated question to explain, but I will try to be as much precise as I can. I am building a calendar (I'm using JTAppleCalendar) with notes. So basically what i'm trying to achieve is to show the calendar and beneath it a tableview which will list all the notes for that specific day. The note are stored in core data. So i have two Entity: AgendaNotes AgendaNoteDates
AgendaNotes has a one to many relationship with AgendaNoteDates (so basically every note can have multiple dates)
The point is to get all the note with the dates and show beneath the selected date the list of the note belonging to that specific date. Each time the user select another day/date the tableview will update and show the note for that day if any exist or a simple text if there are no note.
Until now I have set up the calendar:
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var calendar: JTAppleCalendarView!
var eventsFromTheServer: [String:AgendaEvent] = [:]
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().asyncAfter(deadline: .now()) {
let serverObjects = self.getServerEvents()
for (date, event) in serverObjects {
let stringDate = self.formatter.string(from: date)
self.eventsFromTheServer[stringDate] = event
}
DispatchQueue.main.async {
self.calendar.reloadData()
}
}
here is where I handle the cell selection:
func handleCellSelection(cell: CalendarCell, cellState: CellState) {
let cellDateString = formatter.string(from: cellState.date)
for (title, event) in eventsFromTheServer {
if title == cellDateString {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "CalendarAgendaCell", for: indexPath) as! CalendarAgendaCell
//Fetch model object to display
configuringCell(cell: cell, indexPath: indexPath)
// Return cell
return cell
}
}
}
}
func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) {
let myEvent = myEvents[indexPath.row]
cell.configureCell(agendaEvent: myEvent)
}
// Cell Configuration Functions
func configureCell(cell: JTAppleCell?, cellState: CellState) {
guard let myCustomCell = cell as? CalendarCell else {return}
handleCellSelection(cell: myCustomCell, cellState: cellState)
handleCellEvents(cell: myCustomCell, cellState: cellState)
}
func handleCellEvents(cell: CalendarCell, cellState: CellState) {
// $0 is index value for events from server array, closure says key matches cellState, statement says the event dot view will be hidden if the event from server does not contain value
formatter.dateFormat = "dd MM yyyy"
cell.dotImage.isHidden = !eventsFromTheServer.contains { $0.key == formatter.string(from: cellState.date)}
}
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
selectedDate = date
configureCell(cell: cell, cellState: cellState)
calendar.reloadData()
tableView.reloadData()
}
And here my extension to get the values:
extension CalendarVC {
func getServerEvents() -> [Date:AgendaEvent] {
var myEvent: AgendaEvent!
var eventDate: Date!
for event in myEvents {
for date in (event.agendaDates as? Set<AgendaDate>)! {
let eventDates = date.agendaDates
eventTitle = event.agendaTitle
eventDesc = event.agendaDescription
eventDate = eventDates
myEvent = event
}
}
formatter.dateFormat = "dd MM yyyy"
let eventDateString = formatter.string(from: eventDate)
formatter.dateFormat = "dd MM yyyy"
return [formatter.date(from: eventDateString)! : myEvent ]
}
}
Obviously I have implemented the JTAppleCalendarViewDataSource and the JTAppleCalendarViewDelegate.
This is the first time I'm using JTAppleCalendar and I haven't been able to find any help in the doc. Unfortunately it seems I cannot understand where my mistake is, so even a small pointing in the right direction will be really appreciated!