Swift FSCalendar select 日历中的日期在表视图中显示事件

Swift FSCalendar select date in calendar display event in tableview

我正在为我的应用程序使用 FSCalendar。我需要帮助在我的日历中列出我的集合视图中的特定事件并显示在它下面的表格视图中。因此,当用户单击日历上的单元格(即日期)时。 tableview 将更新并显示当天的特定事件。我已经检查了这个文档,这个特定任务有 none,我还查看了其他几个地方。我会很感激一些代码示例,因为我对使用 FSCalendar 和实现它还很陌生。谢谢

我当前的应用页面:

我的一些相关代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance, UIGestureRecognizerDelegate {

    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var calendar: FSCalendar!
    @IBOutlet weak var animationSwitch: UISwitch!
    @IBOutlet weak var calendarHeightConstraint: NSLayoutConstraint!

    
    fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian)
    
    var datesWithEvent = ["2020-12-23","2020-12-16","2020-12-18","2020-12-14","2020-12-06"]
    var datesWithMultipleEvents = ["2020-12-03","2020-12-13","2020-12-11","2020-10-03","2020-12-06"]
    
    fileprivate lazy var dateFormatter2: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()

override func viewDidLoad() {
        super.viewDidLoad()
        
        calendar.select(Date())
        
        self.view.addGestureRecognizer(self.scopeGesture)
        self.tableView.panGestureRecognizer.require(toFail: self.scopeGesture)
        calendar.scope = .week
//        calendar.scrollDirection = .horizontal
        calendar.accessibilityIdentifier = "calendar"

    }

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        print("did select date \(self.dateFormatter2.string(from: date))")
        let selectedDates = calendar.selectedDates.map({self.dateFormatter2.string(from: [=12=])})
        print("selected dates is \(selectedDates)")
        if monthPosition == .next || monthPosition == .previous {
            calendar.setCurrentPage(date, animated: true)
        }
    }
    
    
    func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
        print("\(self.dateFormatter2.string(from: calendar.currentPage))")
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return [2,20] [section]
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let identifier = ["cell_month", "cell_week"][indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: identifier)!
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
            
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 0 {
            let scope: FSCalendarScope = (indexPath.row == 0) ? .month : .week
            self.calendar.setScope(scope, animated: self.animationSwitch.isOn)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    @IBAction func toggleClicked(sender: AnyObject) {
        if self.calendar.scope == .month {
            self.calendar.setScope(.week, animated: self.animationSwitch.isOn)
        }else {
            self.calendar.setScope(.month, animated: self.animationSwitch.isOn)
        }
        
    }
    
    
    
    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {

        let dateString = dateFormatter2.string(from: date)
                
        if self.datesWithEvent.contains(dateString){
            return 1
        }
        
        if self.datesWithMultipleEvents.contains(dateString) {
            return 2
        }
        
        return 0
    }
    
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
        
        let key = self.dateFormatter2.string(from: date)
        
        if self.datesWithMultipleEvents.contains(key){
            return [UIColor.blue]
        }
        return nil
    }
    
}

我是这样做的: 在我的“didSelect date”方法中,我重新格式化日期,然后调用 Firebase 的处理程序,在其中搜索特定日期。我对 Firebase 使用 wherefield 方法,搜索与该日期匹配的所有事件。这些事件被列在一个数组中。然后我使用该数组列出想要的事件。

在我的Viewcontroller

 func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM-yyyy"
        let result = formatter.string(from: date)
        EventHandler.readEventAtDate(date: result, tableView: eventTable)
    }

在我的处理程序中:

 static func readEventAtDate(date: String, tableView: UITableView) {
    //To feed tableview with specific date
    database.collection("Events").whereField("date", isEqualTo: date).getDocuments { (query, error) in
        if error != nil {
            print("Error: \(String(describing: error))")
        } else {
            self.eventByDate.removeAll()
                for event in query!.documents {
                    let map = event.data()
                    let header = map["header"] as! String
                    let text = map["text"] as! String
                    let place = map["place"] as! String
                    let responsible = map["responsible"] as! String
                    let time = map["time"] as! String
                    let date = map["date"] as! String
                    let coordinates = map["coordinates"] as! GeoPoint
                    
                    let readEvent = Event(eventID: event.documentID, header: header, text: text, place: place, responsible: responsible, time: time, date: date, coordinates: coordinates)
                    eventByDate.append(readEvent)
                }
            DispatchQueue.main.async {
                tableView.reloadData()
            }
        }
    }
}

在我的表格视图中

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return EventHandler.eventByDate.count
}