How to change the backgroungColor cell in tableView with timeInterval using Swift

Viewed 28

I'm trying to change the cell backgroungColor after 3.5 month. I have a textField where i put the date and after 3.5 month of that date I want to change the color of the cell in red.

I tried this where date1 is the date from textField and date2 is this from (isToday) where i have put 106 day = 3.5 month

 let isToday= Date.now.addingTimeInterval(106)

func isSameDay(date1: Date, date2: Date) -> Bool {
    let diff = Calendar.current.dateComponents([.day], from: date1, to: date2)
    if diff.day == 0 {
        return true
    }
    else {
        return false
    }
}

Inside cellForRowAt i have used like this

     var documentSendDate = "05.08.2022"// this is example to be more understandable

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMM/dd/yyyy"
    let date = dateFormatter.date(from: documentSendDate)

    if date != nil {
        let isDayToday = isSameDay(date1: date!, date2: isToday) // here I call the function above

        if isDayToday  == true {

            if customer.isToday == true {
                cell.backgroundColor = .red
            }
        }
    }

But I have this checkBox and when I check it or uncheck it change the color of the random cells. Can someone help me with this please? enter image description here

Here is how i wanted to look.

enter image description here

1 Answers

UITableView is a recycle-list view, so it will reuse the cell UI instance to display data for the corresponding indexPath.

First, modify your code to add a new way of dateFormatter declaration.

// Use lazy var to reduce initialization cost
// Because initializing a new DateFormatter is not cheap, it can consume CPU time like initializing a new NumberFormatter
// lazy var will be only initialized once on the first call/use
lazy var dateFormatter = DateFormatter()

func viewDidLoad() {
    super.viewDidLoad()
    dateFormatter.dateFormat = "MMM/dd/yyyy"
}

It is a reusable UI, so UI won't hold the data or state. The cellForRowAt will be called multiple times when you scroll tableView or when tableView needs to re-layout,... to display the corresponding data-state for each indexPath. That is why you must not initialize or do some big calculations/long waiting here. It will freeze/delay your UI (ref: DispatchQueue.main or MainQueue).

So inside your cellForRowAt function, you need to add logic for all cases if you use switch/if-else.

     var documentSendDate = "05.08.2022"// this is example to be more understandable

    // Here I combine all checks into one if-else
    // Order of check is left-to-right.
    // It is condition1 AND condition2 AND condition3 (swift syntax)
    if let date = dateFormatter.date(from: documentSendDate),
        let isDayToday = isSameDay(date1: date!, date2: isToday),
        customer.isToday == true {
        cell.backgroundColor = .red
    } else {
        cell.backgroundColor = .clear // or your desired color
    }
Related