Search code examples
iosswift3calendarjtapplecalendar

JTAppleCalendar cell background color change error in Swift 3


Im using JTAppleCalendar in my project you can see it here -> https://github.com/patchthecode/JTAppleCalendar. But when I want to change background colors in some cells gives me big problem , when I past previous and next months some cells background colors changing ?

How it is possible ? How can I fix it ? I want to change only; Example ;

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

Ones , but when I turn another month , in calendar looking some cells backgrounds changed.

Under below picture shows true when app opened.

enter image description here

But when I past previous or next month changed some cells backgrounds under below. This is mistake. I don't want to change thats.

enter image description here

My codes under below , where I can mistake ?

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

Solution

  • Finally I resolved issue just I used ;

    cell.contentView.backgroundColor = nil
    

    before;

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

    And which cell item you want to change color, title, etc.. just add before = nil

    After worked, I think will help many people.

    Thank you.