2017-07-07 12 views
1

我有一個UITableViewCell,我在UITableView的幾個UIViewController's中使用。從我的單元格中,有一個對象正在更新,但我想在我的控制器數組中更新它。怎麼樣 ?UITableViewCell中的UIViewController中的數組的更新對象

例如:

的UITableViewCell:

class NotificationTableViewCell: UITableViewCell { 

    @IBOutlet weak var buttonRead: UIButton! 

    var notification = Notification() 

    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 

    @IBAction func clickButtonRead(_ sender: Any) { 
     self.notification.isRead = true 
    } 
} 

的UIViewController:

class NotificationsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITabBarControllerDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    /* List notifications */ 
    var listNotifications = [Notification]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //View cell 
     let nib = UINib(nibName: "viewNotificationTableViewCell", bundle: nil) 
     self.tableView.register(nib, forCellReuseIdentifier: "cellNotification") 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.listNotifications.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     var cell = tableView.dequeueReusableCell(withIdentifier: "cellNotification", for: indexPath) as! NotificationTableViewCell 

     cell.notification = self.listNotifications[indexPath.row] 

     return cell 
    } 
} 

編輯: 我可以通過在控制器和indexPath到cell更新listNotifications中的對象通知像這樣:self.controller.listNotifications[indexPath.row] = self.notification 但是,如果我用這個單元與更多的控制器,我必須從單元投下控制器知道它是什麼控制器。 我正在尋找一種方法來更新對象而不必投射控制器。

+0

請更多的xplain ab你的問題。 –

+0

通知是一個結構體或類? –

+0

通知是一個類。 – MichelRobico

回答

1

您可以通過使用協議 使協議在你的tableview細胞類

protocol NotificationDelegate{ 
     func didReadNotification(notification:Notification , index:Int) 
    } 
    class NotificationTableViewCell: UITableViewCell { 

     @IBOutlet weak var buttonRead: UIButton! 
     // and make this optional don't initialize it 
     var notification:Notification? 
     var delegate:NotificationDelegate? 
     var indexOfNotification:Int? 
     override func awakeFromNib() { 
      super.awakeFromNib() 
     } 

     @IBAction func clickButtonRead(_ sender: Any) { 
      self.notification?.isRead = true 
      self.delegate?.didReadNotification(notification:self.notification! , index:indexOfNotification!) 
     } 

} 

,並在您添加NotificationDelegate的ViewController

class NotificationsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITabBarControllerDelegate , NotificationDelegate { 

     @IBOutlet weak var tableView: UITableView! 

     /* List notifications */ 
     var listNotifications = [Notification]() 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      //View cell 
      let nib = UINib(nibName: "viewNotificationTableViewCell", bundle: nil) 
      self.tableView.register(nib, forCellReuseIdentifier: "cellNotification") 

     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 

     func numberOfSections(in tableView: UITableView) -> Int { 
      return 1 
     } 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return self.listNotifications.count 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      var cell = tableView.dequeueReusableCell(withIdentifier: "cellNotification", for: indexPath) as! NotificationTableViewCell 

      cell.notification = self.listNotifications[indexPath.row] 
      cell.delegate = self 
      cell.indexOfNotification = indexPath.row 
      return cell 
     } 

     // This Delegte method will be called whenever user read the notification 
     func didReadNotification(notification:Notification , index:Int){ 
      // do what ever you want here is your index and notification.. 
      let notification = listNotifications[index] // this is your notification in listNotifications 
     } 

} 
+0

我如何知道listNotifications中的通知是什麼? – MichelRobico

+0

你可以通過索引數字,你可以得到 –

+0

我編輯答案,你可以看看 –

2

完成它的理想方法是創建NotificationTableViewCellDelegate並將委託設置爲視圖控制器。在你的方法中,clickButtonRead調用委託方法並更新視圖控制器中的對象。

protocol NotificationTableViewCellDelegate { 
     func enableReadForNotification(notification: Notification) 
    }  

    class NotificationTableViewCell: UITableViewCell { 

      @IBOutlet weak var buttonRead: UIButton! 
      var notification = Notification() 
      weak var delegate: NotificationTableViewCellDelegate? 

      override func awakeFromNib() { 
       super.awakeFromNib() 
      } 

      @IBAction func clickButtonRead(_ sender: Any) { 
       self.notification.isRead = true 
       if let delegate = self.delegate { 
        delegate.enableReadForNotification(self.notification) 
       } 
      } 
     } 

希望這有助於!

0

做到這一點,你應該在collectionviewcell類中添加一個方法聲明說方法在tableviewcell中按鈕的IBAction,然後從主視圖中的cellForItemAtIndexPath方法調用它