Reputation: 513
Good evening,
I have a UITableView with expanding sections. Its works great, however if I have a section expanded and I select a new section, both sections will be expanded. I am attempting to force close the already opened section so only one section can be opened at a time.
var currentlyOpenedSection = -1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0{
if currentlyOpenSection != indexPath.section && currentlyOpenSection != -1{
// FORCE CLOSE currentlyOpenedSection
Section_List[currentlyOpenSection].isOpened = false // Bool that tracks if this cell is open or not, read by "numberOfRowsInSection" function
}
Section_List[indexPath.section].isOpened = !Section_List[indexPath.section].isOpened // This flip flops the bool that tracks if this section is open, again read by the function "numberOfRowsInSection" to determine how many cells to dedicate to this section
if Section_List[indexPath.section].isOpened {
currentlyOpenSection = indexPath.section // store the new value
} else {
currentlyOpenSection = -1 // or reset the value to show that none are currently open
}
}
tableView.reloadSections([indexPath.section], with: .none)
}
Here is the error message I get: "Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)."
This error message makes complete sense, as I am merely hiding the cells in the section, not adding or deleting cells, but I have no idea how to overcome it.
I found this question on stack overflow from a few years ago, but the only answer merely tells me this is possible, not necessarily how to actually do it. Expand and Contract UITableView row one at a time swift
Any thoughts? Thank you for your time.
Upvotes: 0
Views: 219
Reputation: 513
After typing out my question, I had a thought and decided to try it out and it worked. Since I already typed out the question, I figured I would post it here with the answer.
All I had to do was reload the section after I declared it closed (set isOpened = false). New code below, spacing for emphasis:
var currentlyOpenedSection = -1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0{
if currentlyOpenSection != indexPath.section && currentlyOpenSection != -1{
// FORCE CLOSE currentlyOpenedSection
Section_List[currentlyOpenSection].isOpened = false // Bool that tracks if this cell is open or not
tableView.reloadSections([currentlyOpenedSection], with: .none)
}
Section_List[indexPath.section].isOpened = !Section_List[indexPath.section].isOpened
if Section_List[indexPath.section].isOpened {
currentlyOpenSection = indexPath.section
} else {
currentlyOpenSection = -1
}
}
tableView.reloadSections([indexPath.section], with: .none)
}
Upvotes: 0