2

我有一个有点复杂的问题要解释,但我会尽量做到准确。我正在构建一个带有注释的日历(我正在使用 JTAppleCalendar)。所以基本上我想要实现的是显示日历并在其下方显示一个表格视图,其中将列出该特定日期的所有笔记。注释存储在核心数据中。所以我有两个实体: AgendaNotes AgendaNoteDates

AgendaNotes 与 AgendaNoteDates 有一对多的关系(所以基本上每个笔记都可以有多个日期)

关键是获取所有带有日期的便笺,并在所选日期下方显示属于该特定日期的便笺列表。每次用户选择另一天/日期时,表格视图将更新并显示当天的注释(如果存在)或简单文本(如果没有注释)。

到目前为止,我已经设置了日历:

@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()
        }
}

这是我处理单元格选择的地方:

 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()   
}

在这里我的扩展来获取值:

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 ]
}

}

显然我已经实现了 JTAppleCalendarViewDataSource 和 JTAppleCalendarViewDelegate。

这是我第一次使用 JTAppleCalendar,但我无法在文档中找到任何帮助。不幸的是,我似乎无法理解我的错误在哪里,所以即使是在正确方向上的一个小点也会非常感激!

4

0 回答 0