如何在TableViewCell中显示选定的日期(在日历中)

时间:2020-09-05 17:30:59

标签: swift uitableview calendar fscalendar

我正在开发日历应用程序,并且正在使用FSCalendar库。当我从日历中选择一个日期时,我想在表视图单元格中显示所选日期的事件。我可以在单元格中显示所有事件,但是我正在尝试过滤所选日期的数据集,但是它不起作用。

我正在尝试执行以下操作:示例

我的代码:

import UIKit
import Firebase
import FSCalendar

class CalendarVC: UIViewController, UITableViewDelegate, UITableViewDataSource, FSCalendarDelegate, FSCalendarDataSource {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var calendarView: FSCalendar!

var taskArray = [String]()
var calendarTimeArray = [String]()
var documentIdArray = [String]()
var cellDateString = [String]()

var selectedDate = ""

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    calendarView.delegate = self
    calendarView.dataSource = self
    
    getDataFromFirestore()
    
    let dateToday = Date()
    let formatDate = DateFormatter()
    formatDate.dateFormat = "dd.MM.yyyy"
    let drawDate = formatDate.string(from: dateToday)
    selectedDate = drawDate
}

func getDataFromFirestore() {
    
    let fireStoreDatabase = Firestore.firestore()
    fireStoreDatabase.collection(Auth.auth().currentUser!.email!).order(by: "calendarDate", descending: true).addSnapshotListener { (snapshot, error) in
        if error != nil {
            print(error?.localizedDescription ?? "Error")
        } else {
            if snapshot?.isEmpty != true && snapshot != nil {
                
                self.taskArray.removeAll(keepingCapacity: false)
                self.calendarTimeArray.removeAll(keepingCapacity: false)
                self.documentIdArray.removeAll(keepingCapacity: false)
                self.cellDateString.removeAll(keepingCapacity: false)
                
                for document in snapshot!.documents {
                    let documentID = document.documentID
                    self.documentIdArray.append(documentID)
                    
                    if let title = document.get("calendarTitle") as? String {
                        self.taskArray.append(title)
                    }
                    
                    if let title = document.get("calendarTime") as? String {
                        self.calendarTimeArray.append(title)
                        
                        let dateFormatter = DateFormatter()
                        dateFormatter.locale = NSLocale.current
                        dateFormatter.dateFormat = "d MMM yyyy HH:mm"
                        let date = dateFormatter.date(from:title)!
                        
                        let formatDate = DateFormatter()
                        formatDate.dateFormat = "dd.MM.yyyy"
                        let drawDate = formatDate.string(from: date)
                        self.cellDateString.append(drawDate)
                        
                    }
                    
                }
                self.tableView.reloadData()
            }
        }
    }
}

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    
    let formatter2 = DateFormatter()
    formatter2.dateFormat = "dd.MM.yyyy"
    let string2 = formatter2.string(from: date)
    selectedDate = string2
    
    tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    let compare = cellDateString.filter { element in element == selectedDate }
    return compare.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CalendarCell
    
    cell.taskLabel.text = taskArray[indexPath.row]
    cell.documentIdLabel.text = documentIdArray[indexPath.row]
    
    let cellDate = calendarTimeArray[indexPath.row]
    
    let dateFormatter = DateFormatter()
    dateFormatter.locale = NSLocale.current
    dateFormatter.dateFormat = "d MMM yyyy HH:mm"
    let date = dateFormatter.date(from:cellDate)!
    
    let formatDate = DateFormatter()
    formatDate.dateFormat = "HH:mm"
    let drawDate = formatDate.string(from: date)
    cell.timeLabel.text = drawDate
    
    return cell
}
}

1 个答案:

答案 0 :(得分:1)

当前:

  • FSCalendarDelegate方法仅更新所选日期。

  • numberOfRowsInSection中进行过滤和计数,但是您从不保存过滤的结果,仅计算计数。

  • cellForRowAt中,根本没有应用日期过滤器。

您要做的是让FSCalendarDelegate实际保存过滤后的结果,然后再调用reloadData

但是,拥有四个阵列会使您的工作更加困难。这使得使用一个数组中的值过滤其他三个数组变得非常困难。相反,您应该有一个struct来捕获所有四个值,并具有这些结构的数组。这样,当您过滤(或排序或以其他方式)结果时,所有四个属性将一起处理。

因此,我们定义一个结构来捕获四个属性,而不是四个字符串数组:

struct Event {
    let task: String
    let calendarTime: String
    let documentId: String
    let cellDate: String
}

然后,您可以有一个数组来捕获事件(例如events),并有另一个数组来捕获针对特定日期过滤的事件(例如filteredEvents):

var events: [Event] = []
var filteredEvents: [Event] = []

因此,在解析结果时,创建一个Event对象,并将其附加到events

如果要使用FSCalendarDelegate方法过滤结果,可以执行以下操作:

filteredEvents = events.filter { $0.cellDate == selectedDate }