ios - 在 TableView Controller 中列出计划的通知

标签 ios xcode uitableview swift3

我试图列出用户在我的应用程序中创建和安排的所有通知,类似于苹果“时钟”应用程序中的闹钟列表。但是,每次我收到通知数组并尝试显示它们时,它们并不总是正确显示。

每个通知每天都会在同一时间重复,因此我使用 UNUserNotificationCenter.current().getPendingNotificationRequests 来获取通知数组。通过这个通知数组,我迭代每个通知,创建一个新的自定义“提醒”对象并将其添加到我在 TableView Controller 中显示通知时使用的“提醒”数组。

我每次使用 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
    }
}

当单元格即将显示在 TableView Controller 中时,我使用“Reminder”数组来自定义每个单元格以显示通知信息。这一切都是在“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 方法中打印出通知数量时,它始终是正确的数字。此外,每当我单击应包含通知信息的单元格时,该信息就在那里,只是没有显示。

这是有关这些问题的简短视频。

https://imgur.com/1RVerZD

我不确定如何解决这个问题,我尝试在主队列和全局队列上运行代码,并将服务质量设置为“.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 代码存在一些问题,如下所列。

  1. 您在表格 View 中使用了静态单元格,如果您有动态行,这不是正确的方法。

建议:为您的tableview使用动态原型(prototype)单元格

  1. remindersCount 根本不需要,因为它已经存在于您的数组计数中。

建议:使用 self.reminders.count 进行数组计数。

  • unwindToRemindersTableViewController() 方法有 generateReminders() 调用,这不是必需的,因为 viewWillAppear() 将在 View 关闭时调用。
  • 建议:检查ViewController生命周期,您将正确了解如何重新加载数据。

    我更新了您的示例项目中的一些代码。

    请查找更新后的代码 here .

    Github updated demo

    希望这会有所帮助!

    关于ios - 在 TableView Controller 中列出计划的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54605657/

    相关文章:

    iphone - 处理数组中的多个字符串和整数

    swift - RxSwift "tableView.rx.modelSelected"多次调用

    ios - 通过单元格中的按钮通过 segue 传递数据……不工作……

    ios - UITableViewCell 中的平移手势可以在整个桌面 View 上移动 ImageView ,而无需进入单元格下方

    ios - 如果我使用 prepareForSegue 传递数据,则无法嵌入导航 Controller

    c# - 如何在移动设备上的 Unity3d 中实现多点触控?

    ios - 如何在 iPhone 应用中实现语言翻译?

    SwiftUI - TabView with NavigationView 生成灰色区域

    swift - plist key - UntrustedHostBUID

    ios - 如何在 Storyboard中创建一个按钮以启动在 xcode swift 中使用 spritekit 创建的游戏