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

时间:2017-07-12 10:01:11

标签: swift 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
    }
}

2 个答案:

答案 0 :(得分:0)

您正在进行day比较或实际日期/时间吗? 尝试这样的事情。

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

答案 1 :(得分:0)

我使用Contains方法解决了

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

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

} else {

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

}