I have a tableview cells, and the titles I get from server. Titles are array of strings. The data is drawing like this
- All data
- List item
- Coffees
- Teas
- Dessert
- Main dishes
when I click on each of these I can easily select them and deselect, that part works perfect. Now i want that when i select List item, Coffees, teas, dessert, main dishes together, these 5 items will be deselected and all data(first cell) will be selected automatically.
I know i should write this in didselect method, only i dont know how to write logic correctly. Please help me to solve this. Thanks
this is my code example
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let isSelected = arrayOfTitles[indexPath.row].isSelected else {arrayOfTitles[indexPath.row].isSelected = true; return}
arrayOfTitles[indexPath.row].isSelected = !isSelected
filter?.values?[indexPath.row].isSelected = !isSelected
for i in 1..<arrayOfTitles.count {
if arrayOfTitles[i].isSelected == isSelected {
arrayOfTitles[0].isSelected = isSelected
arrayOfTitles[i].isSelected = !isSelected
}
}
}
2
Answers
You can try
Because a table view maintains its own array of selected rows (
IndexPaths
), you don’t really need to maintain a separate.isSelected
property on your data. If you need it (for state persistence, for example), you can always grab the selected rows when needed.So, here’s another approach…
I’m guessing that if you have some of the rows selected, and you then select the top row, you would also want to deselect the other rows: