在从表格视图删除数据源之前删除确认提醒数据源

问题描述:

我试图在从UITable视图中删除一行之前显示提醒。在从表格视图删除数据源之前删除确认提醒数据源

但我怎么能做到这一点,而无需将我的视图控制器接口传递给数据源。

class BaseTableDataSource: NSObject, UITableViewDataSource { 

     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
      if editingStyle == .Delete { 

      //1. Show alert and delete the block 

      ??? But how can you show the alert with out View controller reference?? 
      //viewcontroller.presentViewController(alertController, animated: true, completion: nil) 
      } 
     } 
    } 

有几种方法可以做到这一点。

1)丑陋的一个:从你的BaseTableDataSource发送NSNotification并在ViewController端捕获它。作为NSNotification的对象 - 您可以传递一个“AlertActionViewModel”或将配置UIAlertActionController的东西

2)您可以通过委托或闭包创建回调。但最终你会放弃BaseTableDataSource的整个想法,所以它不再是唯一的数据源。

希望这会有所帮助。

+0

1.不想使用丑陋的,我宁可不使用数据源。 2.是为什么你创建的数据源丢失的全部点。当他们没有必要的时候,'NSNotification'为' – user431791

+0

-1'。检查此链接:https://www.andrewcbancroft.com/2015/07/16/uitableview-swipe-to-delete-workflow-in-swift/ – kakubei

平时我prefeer创建sharedInstance通知管理器来处理我的警示,但是,即时你可以试试这个方法:

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) 
alert.delegate = self 
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) 
let window = UIWindow(frame: UIScreen.mainScreen().bounds) 
window.rootViewController?.presentViewController(alert, animated: true,completion: nil) 

UPDATE:从你的代码,你也可以做基于

if let tableViewController = tableView.delegate { 
     tableViewController.presentViewController(alert, animated: true,completion: nil) 
} 
+0

背后发布问题的原因是要找出优雅的做法它。我会看看是否有其他人提出更好的答案。 – user431791

+0

我已经更新我的代码,试试这个解决方案..可能会比通过窗口传递更好:) –