I'm still struggling to update data in an UITableView element triggered from an external class using a delegation method. I can already update the table data within the class that contains the table itself (e.g. viewDidLoad()). But I want this table to be updated when a button within another view controller class is tabbed. And yes, I'm a rookie :(
Here is my first class that contains the table to be updated and the method to do so:
class OrderHistoryController: UIViewController, UITableViewDataSource, UITableViewDelegate, reloadTableDataDelegate {
//update table data
func reloadTableData() {
self.orderTable.delegate = self
self.orderTable.dataSource = self
self.orderTable.reloadData()
print("update successful")
}
@IBOutlet var orderTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
reloadTableData()
}
}
This is the protocol for delegation:
protocol reloadTableDataDelegate: class {
func reloadTableData()
}
And this is the class in which the table data update has to be triggered:
class OrderDocumentationController: UIViewController {
weak var delegate: reloadTableDataDelegate?
//delegate function to update table in order history
func updateTableInOrderHistory() {
delegate?.reloadTableData()
}
@IBAction func save(_ sender: UIButton) {
updateTableInOrderHistory()
}
}
The code doesn't cause an error but the delegation simply doesn't work. It seems not to be called and my table does not get updated... Can you please help me? Thanks a lot in advance!!!
It seems the delegate is nil here
delegate?.reloadTableData()
You need to set it inside prepareForSegue