Guest User

Untitled

a guest
Jun 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  2. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MonthCollectionViewCell", for: indexPath) as! MonthCollectionViewCell
  3. cell.backgroundColor = UIColor.gray
  4. let firstDate = dates.first
  5. let index = indexPath.item
  6. let monthDate = Calendar.current.date(byAdding: .month, value: index, to: firstDate as! Date)
  7. let monthInt = Calendar.current.component(.month, from: monthDate!)
  8. let yearInt = Calendar.current.component(.year, from: monthDate!)
  9. cell.monthLabel.text = String(monthInt)+" "+String(yearInt)
  10. cell.year = yearInt
  11. cell.month = monthInt
  12. let monthDates = dates(self.dates as! [Date], withinMonth: monthInt, withinYear: yearInt)
  13. cell.colorViews(monthDates:monthDates)
  14. return cell
  15. }
  16. func dates(_ dates: [Date], withinMonth month: Int, withinYear year: Int) -> [Date] {
  17. let calendar = Calendar.current
  18. let components: Set<Calendar.Component> = [.month,.year]
  19. let filtered = dates.filter { (date) -> Bool in
  20. let monthAndYear = calendar.dateComponents(components, from: date)
  21. return (monthAndYear.month == month && monthAndYear.year == year)
  22. }
  23. return filtered
  24. }
  25.  
  26. class MonthCollectionViewCell: UICollectionViewCell {
  27.  
  28. @IBOutlet weak var monthLabel: UILabel!
  29. @IBOutlet weak var stackView: UIStackView!
  30. var year: Int?
  31. var month: Int?
  32.  
  33. override func awakeFromNib() {
  34. super.awakeFromNib()
  35. for _ in 0...30 { //for the 31 days in a month...
  36. let tick = UIView()
  37. self.stackView?.addArrangedSubview(tick)
  38. }
  39. }
  40.  
  41. func colorViews(monthDates: [Date]){
  42. for date in monthDates {
  43. let dayIndex = Calendar.current.component(.day, from: date)
  44. let tick = stackView.arrangedSubviews[dayIndex]
  45. tick.backgroundColor = UIColor.red
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment