我有一個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