-
-
Save trulyronak/5eadb059faab23dd5cdf2c3dc066afae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in cell for row | |
var day : Date? | |
if (indexPath as NSIndexPath).row >= self.getShiftInSection((indexPath as NSIndexPath).section){ | |
day = self.dateForIndexPath(indexPath) | |
style how you want and check if your date contains date with predicate | |
}else{ | |
//style for blank cell | |
} | |
// In did select row construct the date like above from the indexPath and check if your objects match date with a predicate | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return self.numberOfMonthsToLoad(startDate, endDate: endDate) | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
let offset = self.getShiftInSection(section) | |
let totalDays = self.daysInSection(section) | |
return offset + totalDays | |
} | |
//helpers and calculators | |
func dateForIndexPath(_ indexPath : IndexPath)->Date{ | |
let calendar = Calendar.current | |
let date = self.monthInSection((indexPath as NSIndexPath).section) | |
var yesComponents = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date) | |
yesComponents.day = (indexPath as NSIndexPath).item - self.getShiftInSection((indexPath as NSIndexPath).section) + 1 | |
let newDate = NSDate(from: yesComponents) | |
return newDate as! Date | |
} | |
func getShiftInSection(_ section : Int )->Int{ | |
var offset = 0 | |
if currentSection == nil || section != currentSection{ | |
let sectionDate : Date = Calendar.current.date(byAdding: .month, value: -section, to: endDate)! | |
var oldComp = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.weekday], from: sectionDate) | |
oldComp.day = 1 | |
let newDay = NSDate(from: oldComp) | |
let newComponents = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.weekday], from: newDay as! Date) | |
offset = newComponents.weekday! - 1 | |
currentSection = section | |
self.currentOffset = offset | |
}else{ | |
offset = self.currentOffset | |
} | |
// adjust offset | |
if offset == 7{ | |
return 0 | |
}else if offset > 7{ | |
return offset - 7 | |
}else{ | |
return offset | |
} | |
} | |
func getMonthForSection(_ dateIndex : Int)->Int{ | |
var comp = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: endDate) | |
comp.month = comp.month! - dateIndex | |
return comp.month! | |
} | |
func monthInSection(_ dateIndex: Int)->Date{ | |
var comp = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: endDate) | |
comp.month = comp.month! - dateIndex | |
return NSDate(from: comp) as Date | |
} | |
func daysInSection(_ section :Int)->Int{ | |
var componets = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.weekday], from: endDate) | |
componets.month = componets.month! - section | |
return NSDate.numberOfDays(inMonth: componets.month!, forYear: componets.year!) | |
} | |
func getMonthAsDateForSection(_ dateIndex : Int)->Date{ | |
var comp = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: endDate) | |
comp.month = comp.month! - dateIndex | |
return NSDate(from: comp) as Date | |
} | |
func isToday(_ date : Date)-> Bool{ | |
if date == self.today{ | |
return true | |
}else{ | |
return false | |
} | |
} | |
func doesDatesContainDate(_ date : Date)->Bool{ | |
if dates.contains(date){ | |
return true | |
}else{ | |
return false | |
} | |
} | |
func numberOfMonthsToLoad(_ startDate : Date, endDate : Date)->Int{ | |
//start and end date | |
let time = Calendar.current.dateComponents([.month], from: startDate, to: endDate).month | |
if let tm = time{ | |
return tm + 1 | |
}else{ | |
return 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
revisit the code as I have added lots of missing bits and fixed some old syntax. it is more up to date but the only difference is that it is for NSCollectionView instead of UICollectionView. Just change the necessary parts and it will work. Good luck. https://gist.github.com/agibson73/8f76d4a9d4381e432a4e94af7f01dd4e