我JTAppleCalendar
在我的项目中使用你可以在这里看到它-> https://github.com/patchthecode/JTAppleCalendar。但是当我想改变某些单元格中的背景颜色给我带来了很大的问题,当我过去和下个月某些单元格的背景颜色发生变化时?
怎么可能?我该如何解决?我只想改变;例子 ;
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"Mains")
let predicate = NSPredicate (format:"date = %@",freshdate)
fetchRequest.predicate = predicate
if let result = try? context.fetch(fetchRequest) as! [Mains] {
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
}
一个,但是当我再转一个月时,在日历中看起来一些单元格的背景发生了变化。
下图显示应用打开时为真。
但是当我过去上个月或下个月改变了下面的一些单元格背景时。这是错误。我不想改变那个。
我的代码在下面,我会在哪里弄错?
@IBOutlet weak var calendarView: JTAppleCalendarView!
let kStartDate = "2016-01-01"
let kEndDate = "2049-12-31"
var numberOfRows = 6
let formatter = DateFormatter()
var myCalendar = Calendar(identifier: .gregorian)
var generateInDates: InDateCellGeneration = .forAllMonths
var generateOutDates: OutDateCellGeneration = .off
var hasStrictBoundaries = true
let firstDayOfWeek: DaysOfWeek = .monday
var monthSize: MonthSize? = nil
extension ViewController: JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US")
let startDate = formatter.date(from: kStartDate)!
let endDate = formatter.date(from: kEndDate)!
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: numberOfRows,
calendar: myCalendar,
generateInDates: generateInDates,
generateOutDates: generateOutDates,
firstDayOfWeek: firstDayOfWeek,
hasStrictBoundaries: hasStrictBoundaries)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
let comedate = String(describing: myCalendar.date(byAdding: .day, value: 1, to: cellState.date))
var freshdate = comedate.substring(from: 9)
freshdate = freshdate.substring(to: 10)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"Mains")
let predicate = NSPredicate (format:"date = %@",freshdate)
fetchRequest.predicate = predicate
if let result = try? context.fetch(fetchRequest) as! [Mains] {
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
}
handleCellConfiguration(cell: cell, cellState: cellState)
return cell
}
}