2017-07-07 31 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方法调用它

相关问题