【问题标题】:Alarm clock app, deleting a specific local notification from a UItableView in swift闹钟应用程序,快速从 UItableView 中删除特定的本地通知
【发布时间】:2015-01-12 15:34:49
【问题描述】:

我正在尝试使用 UILocalNotification 构建闹钟。我可以安排警报并将它们填充到 UITableView 中。通知警报工作正常,我可以轻松取消所有已安排的通知。我的问题是:

  1. 如何从表中删除特定的本地通知?
  2. 我怎样才能防止通知在被解雇后像苹果闹钟一样在被解雇后从桌子上消失。

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
    
    var notificationArray:NSArray = UIApplication.sharedApplication().scheduledLocalNotifications
    var notification:UILocalNotification = notificationArray.objectAtIndex(indexPath.row) as UILocalNotification
    
    UIApplication.sharedApplication().cancelLocalNotification(notification)
    
    alarmTable.reloadData()
    
       }
    }
    

【问题讨论】:

    标签: uitableview swift ios8 uilocalnotification alarm


    【解决方案1】:

    如果要删除一行,请使用

    tableView.beginUpdates()
    tableView.deleteRowsAtIndexPaths([ indexPathRowToDelete ], withRowAnimation: UITableViewRowAnimation.Fade)
    tableViewMain.endUpdates()
    

    你不需要使用reloadData,因为函数会处理这个问题。

    但是,如果您想让条目处于非活动状态,例如不要删除它,为每个数据条目使用一个变量来显示它是否处于活动状态。检查cellForRowAtIndexPath 方法中的标志。然后您可以相应地设置标志并调用reloadRowsAtIndexPaths

    旁注:访问UIApplication.sharedApplication() 来检索数据并不是一个好主意。最好将数据传递给表格视图的数据源。

    【讨论】:

    • 很抱歉,您能否解释更多,我无法实施您的建议。我试过并得到了这个“***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIConcreteLocalNotification row]:无法识别的选择器发送到实例0x7fcf905a00d0'”
    【解决方案2】:

    我找到了解决问题的方法。据我了解,在删除本地通知并重新加载表时,它会崩溃,因为它假定已删除通知的行仍然存在。所以我所做的是我添加了一个命令来删除删除本地通知命令下的行。这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2014-11-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多