I use this if condition inside of didSelectRowAt method
That seems to be the critical part of your code.
In your code, item is a local variable. Changing the property of it does not affect the instance property food.
Please try something like this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
foods[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .automatic)
}
UITableView manages cell selection internally. So, you may need to add some code to make your isSelected work with such internal selection management, but anyway, please try the code above and tell us what happens.
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: