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