Jtapplecalendar: Highlight day if the date equal to date in array

Created on 12 Jul 2017  路  15Comments  路  Source: patchthecode/JTAppleCalendar

I have an array of object contains bookings

class MonthBookings: NSObject {

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

var bookings = [MonthBookings]()

So I need to check if cell date is equal to some date in bookings array then change cell color for this date in cell.

I have tried this way down below but didn't work:

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

Most helpful comment

Guys, I solved this problem like this.
I have array of dictionaries with 'date' // dates are that array which I get from server

// Format Cell Date without 'hh:mm:ss' , e.g 2017-07-15
let currentDate = CBDateFormatter.getCurrenEventDate(with: cellState.date)

// if you want that dictionary
if let dictionary = dates.first(where: { $0["date"] as? String == currentDate }) {

   // Do something when equal dates 
   cell.backgroundColor = .red
   print(dictionary)

}
else {
    cell.backgroundColor = .clear
}
Or without dictionary like this 
if dates.contains(where: { $0["date"] as? String == currentDate }) {

// Do something when equal dates 
   cell.backgroundColor = .red
        }

        else {
           cell.backgroundColor = .clear
        }

Thank you so much for fast response

All 15 comments

Please answer this question , I have same problem for this

try to use a dictionary inplace of an array if possible. = less looping
try to compare date strings instead of actual date values. This is easier especially if you format the string without hh:mm:ss

Guys, I solved this problem like this.
I have array of dictionaries with 'date' // dates are that array which I get from server

// Format Cell Date without 'hh:mm:ss' , e.g 2017-07-15
let currentDate = CBDateFormatter.getCurrenEventDate(with: cellState.date)

// if you want that dictionary
if let dictionary = dates.first(where: { $0["date"] as? String == currentDate }) {

   // Do something when equal dates 
   cell.backgroundColor = .red
   print(dictionary)

}
else {
    cell.backgroundColor = .clear
}
Or without dictionary like this 
if dates.contains(where: { $0["date"] as? String == currentDate }) {

// Do something when equal dates 
   cell.backgroundColor = .red
        }

        else {
           cell.backgroundColor = .clear
        }

Thank you so much for fast response

I have the same problem. Where did you implement this?

You can do it in datasource function

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell

but using in Custom cell class best practice.

For example

cell.fill(with: cellState, dates: dates)

Hope it'll help .
Say me if you have any question

is your 'dates' array Any type?

No, like this
var dates = [Dictionary<String, AnyObject>] ()

I get it from server , the request manager give me all dates from Server , I parse it like this

 calendarRequestManager.getEventsOnCalendar(with: urlTail,
                                         success: { datesArray in

                                            var dates = [Dictionary<String, AnyObject>]()
                                            var dateDict = Dictionary<String, AnyObject>()

                                            for dictionary in datesArray {

                                                if let dateString = dictionary["created_date"] {

                                                    let date = CBDateFormatter.eventDate(from: dateString as! String)
                                                    dateDict["date"] = date as AnyObject
                                                }

                                                if let categoryId = dictionary["category_id"] {

                                                    dateDict["categoryId"] = categoryId as AnyObject
                                                }

                                                dates.append(dateDict)
                                            }

                                            success?(dates)


        }, failure: { error in

            failure?(error)
        })

Thanks,, In my case I am getting start date and end date in the array of dictionaries. And I want to highlight in between dates. Do you have any idea to implement that?

@johnharutyunyan you're awesome, thanks for solution

@RoshaniDias, I think better solution is get key / allKeys from dictionary and compare with start and end date.
Let me know if I can help .

let dateKey = dictionary["some_key"] as! String

if startDate < dateKey && dateKey < endDate {

   // Do that you want
cell.backgroundColor = .purple
}
else {

   cell. backGroundColor = .clear
}

@ahmedraad , you're welcome , thank you too

@johnharutyunyan @ahmedraad @RoshaniDias i think he put it in this video -> https://www.youtube.com/watch?v=CQNotydm58s

@zuaaef cool , thank you!

closing this issue. If there are still problems here, let me know and i will re-open this and help solve it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dimitris-c picture dimitris-c  路  5Comments

zhanswift picture zhanswift  路  5Comments

programus picture programus  路  3Comments

dbmrq picture dbmrq  路  5Comments

programus picture programus  路  5Comments