I have a UITableViewController
with custom UITableViewCell
, and every cell has an UISwitch
inside.
Need to update an information on my table (a string in the header), when any of these switches turn on/off (need to display the number of switches on in the section header).
I'm not confident with iOS and UIKit, but I've already found 2 possibile solutions:
- implement a delegate pattern between cell and table controller
- write update logic inside the function
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
In the first solution my TableViewController
conforms to
protocol TableViewDelegate {
func reloadTable()
}
that update its inner counter of switches on - and update the header section in function tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
- calling
DispatchQueue.main.async{
self.tableView.reloadData()
}
Obviously my custom UITableViewCell
has the reference to the delegate TableViewController
and call its reloadTable()
.
The second solution instead is about the possibility to get the information of every cell in the method cellForRowAt indexPath
. I've found that this function in not only called when the table has to be drawn, but also when I interact with a component into a cell.
So I need to implement a counting in the function cellForRowAt indexPath
? Or the first solution with delegate pattern is a good one?