Swift 4.2 EventKit获取TableView上选中日历的所有Events didSelectedRowAt
Swift 4.2 EventKit get all Events to the selected calendar on TableView didSelectedRowAt
我正在将我所有的日历加载到一个表格视图中,其中每个日历都在一个单元格中表示。
现在我想在点击单元格时打印单元格日历的所有事件。
问题是,根据您看到的代码,我得到了 all 日历的事件。
如何让函数只查看我在 tableview 中点击的日历?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let calendarTitle = calendars![indexPath.row].title
print(calendarTitle)
let calendar = Calendar.current
print("Current \(calendar)")
var oneDayAgoComponents = DateComponents()
oneDayAgoComponents.day = -1
let oneDayAgo = calendar.date(byAdding: oneDayAgoComponents, to: Date())
var oneDayFromNowComponents = DateComponents()
oneDayFromNowComponents.day = 7
let oneDayFromNow = calendar.date(byAdding: oneDayFromNowComponents, to: Date())
var predicate: NSPredicate? = nil
if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: nil)
}
var events: [EKEvent]? = nil
if let aPredicate = predicate {
events = eventStore.events(matching: aPredicate)
for event in events! {
print(event.title)
}
}
}
您必须在 predicateForEvents
的 calendars
参数中传递日历
let rowCalendar = calendars![indexPath.row]
let calendarTitle = rowCalendar.title
print(calendarTitle)
...
if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: [rowCalendar])
}
并且请将数据源数组声明为非可选空数组,因为您无论如何都要强制展开它
var calendars = [EKCalendar]()
我正在将我所有的日历加载到一个表格视图中,其中每个日历都在一个单元格中表示。 现在我想在点击单元格时打印单元格日历的所有事件。 问题是,根据您看到的代码,我得到了 all 日历的事件。
如何让函数只查看我在 tableview 中点击的日历?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let calendarTitle = calendars![indexPath.row].title
print(calendarTitle)
let calendar = Calendar.current
print("Current \(calendar)")
var oneDayAgoComponents = DateComponents()
oneDayAgoComponents.day = -1
let oneDayAgo = calendar.date(byAdding: oneDayAgoComponents, to: Date())
var oneDayFromNowComponents = DateComponents()
oneDayFromNowComponents.day = 7
let oneDayFromNow = calendar.date(byAdding: oneDayFromNowComponents, to: Date())
var predicate: NSPredicate? = nil
if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: nil)
}
var events: [EKEvent]? = nil
if let aPredicate = predicate {
events = eventStore.events(matching: aPredicate)
for event in events! {
print(event.title)
}
}
}
您必须在 predicateForEvents
calendars
参数中传递日历
let rowCalendar = calendars![indexPath.row]
let calendarTitle = rowCalendar.title
print(calendarTitle)
...
if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: [rowCalendar])
}
并且请将数据源数组声明为非可选空数组,因为您无论如何都要强制展开它
var calendars = [EKCalendar]()