Ios 单击日历时显示从日期开始的事件

Ios 单击日历时显示从日期开始的事件,ios,uitableview,calendar,swift2,nsdate,Ios,Uitableview,Calendar,Swift2,Nsdate,在我的应用程序中,我使用JTAppleCalendar()来显示日历。我还使用核心数据保存事件,其中包含一些信息。此信息还包括日期。目标是能够单击某个日期,并将包含该日期的任何事件显示在CalendarViewController容器中的tableView中。现在我可以点击一个有事件的日期,tableView将显示所有过去的事件(如果该日期在过去),并将显示所有未来的事件(如果该日期在未来)。以下是CalendarViewController中的代码: var showPastEvents =

在我的应用程序中,我使用JTAppleCalendar()来显示日历。我还使用核心数据保存事件,其中包含一些信息。此信息还包括日期。目标是能够单击某个日期,并将包含该日期的任何事件显示在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 {
    }
}
}
(EventPastsStringDate是一个datePicker.date,当事件实际保存在不同的屏幕中时,它使用与上述相同的格式化程序转换为字符串)

然后在CalendarContainerView中,它将根据ShowPasteEvents=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以显示正确的单元格数。有什么想法吗?我知道这是很多代码,但我觉得这将是有益的

男人。。太长了。。让人困惑的方式。用户点击一个dateCell,你希望你的tableView显示什么?存储在该日期的核心数据中的事件我意识到这太长了,我打算删除它,但我只是问了一个简单得多的问题,这与我希望你查看我的个人资料的内容几乎相同,所以我只是想确认一下。你的问题和日历有关吗?还是与核心数据有关?因为,我猜您已经在某个数组或变量中获得了关于该日期的核心数据信息,对吗?那么,这个问题与日历有关吗?或核心数据?有关核心数据的更多信息,以及如何检查存储的事件是否具有与所选日期相同的日期属性,然后获取要显示的事件的索引