Im trying to find todays Date() and change color of cell

Im not sure what Im doing wrong here...
Im trying to pull todays date and change the color of that cell form a collectonView.
Below is my helper file.

	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return totalSquares.count
	}
	
	func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell
		
		
		cell.dayOfMonth.text = totalSquares[indexPath.item]
		
		// My code
		let date = Date()
		let calendar = Calendar.current
			
		if indexPath.row == (Calendar.current.date)! {
				
				cell.backgroundColor = .red
			
		}
		// End My code
		return cell

Here is my Helper File:

class CalendarHelper
{
	
	let calendar = Calendar.current
	
	func plusMonth(date: Date) -> Date
	{
		return calendar.date(byAdding: .month, value: 1, to: date)!
	}
	
	func minusMonth(date: Date) -> Date
	{
		return calendar.date(byAdding: .month, value: -1, to: date)!
	}
	
	func monthString(date: Date) -> String
	{
		let dateFormatter = DateFormatter()
		dateFormatter.dateFormat = "LLLL"
		return dateFormatter.string(from: date)
	}
	
	func yearString(date: Date) -> String
	{
		let dateFormatter = DateFormatter()
		dateFormatter.dateFormat = "yyyy"
		return dateFormatter.string(from: date)
	}
	
	func daysInMonth(date: Date) -> Int
	{
		let range = calendar.range(of: .day, in: .month, for: date)!
		return range.count
	}
	
	func dayOfMonth(date: Date) -> Int
	{
		let components = calendar.dateComponents([.day], from: date)
		return components.day!
	}
	
	func firstOfMonth(date: Date) -> Date
	{
		let components = calendar.dateComponents([.year, .month], from: date)
		return calendar.date(from: components)!
	}
	
	func weekDay(date: Date) -> Int
	{
		let components = calendar.dateComponents([.weekday], from: date)
		return components.weekday!-1
	}
	
}

First off, what is the behavior you are seeing? Are no rows being highlighted? The wrong row? It's hard to know what's wrong in the snippets you posted without understanding what actually happened.

Hmm, what is the definition of Calendar.current.date? That's not part of the normal Calendar type. Did you define that in an extension? It seems like this code assumes the value of that expression will be equal to the row number. If date returns something that is 1-based (first day of the month is 1) then this definitely won't highlight the right line because index path rows are 0-based (first row is 0).

I am just trying to create a "Today" button that will take me back to the current date.

Sorry, I don't know how to highlight.

The lines of code Im referring that isn't working for me, I commented out with "My Code".
I also added the helper file that shows the date formatters.

I'd recommend starting by putting a breakpoint on this line:

See if this line is even being executed. If not, then it's likely that the conditional, indexPath.row == (Calendar.current.date)!, is the culprit. So far it seems like it all comes down to what Calendar.current.date returns. Did you add a .date property to Calendar in an extension?

Terms of Service

Privacy Policy

Cookie Policy