在我的应用中,我使用JTAppleCalendar(https://github.com/patchthecode/JTAppleCalendar)来显示日历。我还使用Core Data来保存包含一些信息的事件。此信息还包括日期。目标是能够单击日期并使包含该日期的任何事件显示在CalendarViewController中容器中的tableView中。现在我可以点击有事件的日期,tableView将显示所有过去的事件(如果该日期是过去的事件),并将显示所有未来事件(如果该日期在未来)。下面是CalendarViewController中的代码:
var showPastEvents = Bool() // Allows the CalendarContainView to access these
var showFutureEvents = Bool() // Allows the CalendarContainView to access these
extension CalendarVC: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate {
func calendar(calendar: JTAppleCalendarView, didSelectDate date: NSDate, cell: JTAppleDayCellView?, cellState: CellState) {
(cell as? CellView)?.cellSelectionChanged(cellState)
//printSelectedDates()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy MM dd"
// Allows access to CalendarEventsCVC
let CalendarCVC: CalendarEventsCVC = self.childViewControllers[0] as! CalendarEventsCVC
// Clicking on any date will tell CalendarEventsCVC to not show any events unless otherwise told to
showPastEvents = false
showFutureEvents = false
CalendarCVC.tableView.reloadData()
// Search for any dates that are the same as eventPastStringDate
let requestPast = NSFetchRequest(entityName: "EventsPast")
requestPast.predicate = NSPredicate(format: "eventPastStringDate = %@", formatter.stringFromDate(date))
requestPast.returnsObjectsAsFaults = false
do {
let resultsPast = try mocPast.executeFetchRequest(requestPast)
if resultsPast.count > 0 {
for result in resultsPast as! [NSManagedObject] {
if let eventPastStringDate = result.valueForKey("eventPastStringDate") as? String {
// Code will run here only if the date selected is an eventPastStringDate
if eventPastStringDate == formatter.stringFromDate(date) {
if date.earlierDate(currentDate).isEqualToDate(date) {
showPastEvents = true
showFutureEvents = false
print("showPastEvents = true")
CalendarCVC.tableView.reloadData()
} else if date.isEqualToDate(currentDate) {
showPastEvents = true
showFutureEvents = false
print("showPastEvents = true")
CalendarCVC.tableView.reloadData()
}
} else if eventPastStringDate != formatter.stringFromDate(date) {
showPastEvents = false
showFutureEvents = false
print("Hello")
CalendarCVC.tableView.reloadData()
}
}
}
}
} catch {
}
// Search for any dates that are the same as eventFutureStringDate
let requestFuture = NSFetchRequest(entityName: "EventsFuture")
requestFuture.predicate = NSPredicate(format: "eventFutureStringDate = %@", formatter.stringFromDate(date))
requestFuture.returnsObjectsAsFaults = false
do {
let resultsFuture = try mocPast.executeFetchRequest(requestFuture)
if resultsFuture.count > 0 {
for result in resultsFuture as! [NSManagedObject] {
if let eventFutureStringDate = result.valueForKey("eventFutureStringDate") as? String {
// Code will run here only if the date selected is an eventFutureStringDate
if eventFutureStringDate == formatter.stringFromDate(date) {
if date.laterDate(currentDate).isEqualToDate(date) {
showFutureEvents = true
showPastEvents = false
print("showFutureEvents = true")
CalendarCVC.tableView.reloadData()
} else if date.isEqualToDate(currentDate) {
showPastEvents = true
showFutureEvents = false
print("showPastEvents = true")
CalendarCVC.tableView.reloadData()
}
} else if eventFutureStringDate != formatter.stringFromDate(date) {
showPastEvents = false
showFutureEvents = false
print("Hello")
CalendarCVC.tableView.reloadData()
}
}
}
}
} catch {
}
}
}
(eventPastStringDate是一个datePicker.date,当事件实际保存在不同的屏幕中时,使用与上面相同的格式化程序转换为字符串)
然后在CalendarContainerView中,它将仅根据showPastEvents = true或showFutureEvents = true显示某些事件。下面是代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let result: UITableViewCell
if eventsPast.count > 0 && showPastEvents == true {
let ePast = eventsPast[indexPath.row]
let cellPast = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
// Sets labels in cell to whatever user typed in on the events page
cellPast.titlePrototypeCell!.text = ePast.eventPastTitle!
// Other Information Also
result = cellPast
} else if eventsFuture.count > 0 && showFutureEvents == true {
let eFuture = eventsFuture[indexPath.row]
let cellFuture = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
// Sets labels in cell to whatever user typed in on the events page
cellFuture.titlePrototypeCell!.text = eFuture.eventFutureTitle!
// Other Information Also
result = cellFuture
} else {
let noEventCell = self.tableView.dequeueReusableCellWithIdentifier("noEventCell", forIndexPath: indexPath)
result = noEventCell
}
return result
}
我在想如果我可以获取eventPastStringDate / eventPastFutureDate所在事件的indexPath,那么我只能在CalendarContainerView中显示该索引,但我不知道如何获取它。此外,我将不得不更改numberOfRowsInSection以显示正确的单元格数。有任何想法吗?我知道这是很多代码,但我觉得这将是有益的。