我有一个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
但是,如果我用这个单元与更多的控制器,我必须从单元投下控制器知道它是什么控制器。 我正在寻找一种方法来更新对象而不必投射控制器。
请更多的xplain ab你的问题。 –
通知是一个结构体或类? –
通知是一个类。 – MichelRobico