【发布时间】:2016-11-22 04:09:37
【问题描述】:
想在检索提醒方面寻求帮助。似乎 numberOfRowsInSection 甚至在 eventStore.fetchReminders 完成之前就被调用了。这是我的代码:
override func viewWillAppear(_ animated: Bool) {
print("****viewWillAppear")
super.viewWillAppear(true)
self.eventStore = EKEventStore()
self.reminders = [EKReminder]()
//Added to initialize
self.reminders.removeAll()
self.eventStore!.requestAccess(to: EKEntityType.reminder, completion:
{(granted, error) in
if granted
{
let predicate = self.eventStore.predicateForReminders(in: nil)
self.eventStore.fetchReminders(matching: predicate, completion: { (reminders: [EKReminder]?) -> Void in
self.reminders = reminders
DispatchQueue.main.async() {
//Added
self.reminders = reminders
self.tableView.reloadData()
}
})
}
else
{
print("The app is not permitted to access reminders, make sure to grant permission in the settings and try again")
}
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("*****numberOfRowsInSection")
print("numberOfRowsInSection reminders \(self.reminders.count)")
return self.reminders.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
print("****titleForHeaderInSection")
print("titleForHeaderInSection reminders \(self.reminders.count)")
return "test"
}
我尝试将其放入 viewDidLoad 但同样的事情发生了,numberofRowsInSection 中的 self.reminders.count 返回 0。我还尝试在 requestAccess 的外部调用 fetchReminders(因为我认为 requestAccess 没有触发),但我最终得到一样的东西。有什么我做错了吗? 非常感谢任何建议!
谢谢!
【问题讨论】:
-
您可以尝试在您的 viewWillAppear 中添加 super.viewWillAppear() 吗?您还应该在 viewDidLoad() 中调用以下内容:self.eventStore!.requestAccess(to: EKEntityType.reminder, completion:
-
嗨,迈克尔,谢谢!我添加了 super.viewWillAppear。我还在 viewDidLoad 中添加了 requestAccess。我得到了相同的结果:(
-
至少你在这里得到了更好的代码。即使它没有解决您的问题;)很高兴看到您可以解决它。
标签: swift swift3 reminders ekeventstore