如果日期等于数组中的日期 (JTAppleCalendar),则突出显示日期

Highlight day if the date equal to date in array (JTAppleCalendar)

我有一个包含预订的对象数组

class MonthBookings: NSObject {

    var date: Date = Date()    
    var bookings_count: Int = 0
}

var bookings = [MonthBookings]()

所以我需要检查单元格日期是否等于预订数组中的某个日期,然后更改单元格中该日期的单元格颜色。

我在下面尝试过这种方法,但没有成功:

func calendar(_ calendar: JTAppleCalendarView, willDisplayCell cell: JTAppleDayCellView, date: Date, cellState: CellState) {
    guard let sfbCell = cell as? SFBCalendarCell else {return}

    let booking = self.bookingsMonths[cellState.row()]

    if booking.date == date {
        sfbCell.dayText.textColor = .red
    } else {
        sfbCell.dayText.textColor = .black
    }
}

您是在做 day 比较还是实际 date/Time? 尝试这样的事情。

let order = CalendarManager.currentCalendar.compare(cellState.date, to: 
Date(), toGranularity: .day)
  if order == .orderedSame {
    calendarCell.dayNumberLabel.textColor = UIColor.white
  }

我使用 Contains 方法解决了

if bookingsMonths.contains(where: { [=10=].date == cellState.date && [=10=].bookings_count != 0 }) {

      sfbCell.dayText.font = UIFont.boldSystemFont(ofSize: 15)

} else {

      sfbCell.dayText.font = UIFont.systemFont(ofSize: 15)

}