ios - 在表视图控制器中列出计划的通知
问题描述
我正在尝试列出用户在我的应用程序中创建和安排的所有通知,类似于 Apple 的“时钟”应用程序中的警报列表。但是,每次我收到通知数组并尝试显示它们时,它们都不会一直正确显示。
每个通知每天都在同一时间重复,所以我UNUserNotificationCenter.current().getPendingNotificationRequests
用来获取通知数组。使用这个通知数组,我遍历每个通知,创建一个新的自定义“提醒”对象并将其添加到我在表格视图控制器中显示通知时使用的“提醒”数组中。
我每次使用 viewWillAppear 函数都会这样做。
这是代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
generateReminders()
tableView.reloadData()
}
func generateReminders()
{
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests { (notifications) in
for item in notifications {
if let trigger = item.trigger as? UNCalendarNotificationTrigger,
let triggerDate = trigger.nextTriggerDate() {
var withSound = true
if(item.content.sound != UNNotificationSound.default)
{
withSound = false
}
self.reminders.append(Reminder(identifier: item.identifier, time: triggerDate, message: item.content.body, withSound: withSound, isAPendingNotification: true))
}
}
self.remindersCount = notifications.count
}
}
当单元格即将显示在表格视图控制器中时,我使用“提醒”数组来自定义每个单元格以显示通知的信息。这一切都在“cellForRowAt”函数中完成,代码如下。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reminder", for: indexPath)
var text = ""
var detailText = ""
if(indexPath.row < remindersCount) {
let reminder = reminders[indexPath.row]
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
text = formatter.string(from: reminder.Time)
detailText = reminder.Message
}
cell.textLabel?.text = text
cell.detailTextLabel?.text = detailText
return cell
}
当用户选择要查看的另一个选项卡时,我将“提醒”对象重置为空,以便当他们返回此选项卡时,会显示更新的通知数组,代码如下。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
reminders = [Reminder]()
remindersCount = 0
tableView.setNeedsDisplay()
}
我面临的问题是这是非常不一致的,有时会显示所有通知,有时只显示一些通知,有时没有显示。但是,每次我打印出UNUserNotificationCenter.current().getPendingNotificationRequests
方法中通知数量的计数时,它总是正确的数字。此外,每当我单击应包含有关通知的信息的单元格时,该信息就在那里,只是没有显示。
这是有关这些问题的简短视频。
我不确定如何解决这个问题,我试图在主队列和全局队列上运行代码,并将服务质量设置为“.userInteractive”,如下所示,但是仍然没有骰子。
let center = UNUserNotificationCenter.current()
let dq = DispatchQueue.global(qos: .userInteractive)
dq.async {
center.getPendingNotificationRequests { (notifications) in
for item in notifications {
if let trigger = item.trigger as? UNCalendarNotificationTrigger,
let triggerDate = trigger.nextTriggerDate() {
var withSound = true
if(item.content.sound != UNNotificationSound.default)
{
withSound = false
}
self.reminders.append(Reminder(identifier: item.identifier, time: triggerDate, message: item.content.body, withSound: withSound, isAPendingNotification: true))
}
}
self.remindersCount = notifications.count
}
}
可以从此 Github 存储库下载发生此问题的小应用程序。
https://github.com/AlexMarchant98/LitstingNotificationsIssue
解决方案
您的代码中存在一些问题,tableview
如下所示。
- 您在表格视图中使用了静态单元格,如果您有动态行,这不是正确的方法。
建议:为您的tableview
.
remindersCount
根本不需要,因为它已经存在于您的数组计数中。
建议:self.reminders.count
用于数组计数。
unwindToRemindersTableViewController()
方法有generateReminders()
调用,这不是必需的,因为viewWillAppear()
视图关闭时会调用。
建议:检查ViewController生命周期,您将正确了解如何重新加载数据。
我已经更新了您的示例项目中的一些代码。
请在此处找到更新的代码。
希望这会有所帮助!
推荐阅读
- c++ - Boost VF2 子图同构对于无向图不正确?
- python - 遍历列表时处理错误
- python - Django:需要帮助在同一视图中返回 2 组数据
- python - 使用正则表达式匹配具有奇数个 0 和偶数个 1 的二进制字符串
- java - Apache HttpClient 中的自签名证书问题
- swift - Swift Combine:使用类似滑动窗口的功能进行收集
- php - 正则表达式以样式显示>引号
- firebase - 有 where 子句时 React Native Firebase 异步函数退出
- excel - 获取一天最后一个条目的值
- c++ - 为什么 WriteFile 不为该代码中的后续写入提前文件偏移量?