2016-04-28 65 views
0

我試圖在從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

有幾種方法可以做到這一點。

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

0

平時我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

我已經更新我的代碼,試試這個解決方案..可能會比通過窗口傳遞更好:) –