Reputation: 89
I have a switch button to hide and show a section in table view
lets say I have three section that defined in a array:
var sections = ["section1", "section2", "section3"]
and here is table view protocols for sections
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
and here is the function that will be called when the switch button toggle will change:
@objc func switchStateDidChange(_ sender: UISwitch) {
sections.remove(at: 1)
// My problem is here
tableView.deleteSections(IndexSet, with: .top)
}
In this function, first, I said that the number of sections in the section array will be reduced by one, then delete the section 1
but if I put 1 in IndexSet, I get this error:
Cannot convert value of type 'Int' to expected argument type 'IndexSet'
How I can tell to tableView.deleteSections
to remove section 1?
Upvotes: 1
Views: 2131
Reputation: 1529
You need to make sure you do a couple of things when updating the UITableView
.
Make sure you have removed the data from the array that the tableView
is using
Make sure that when you update the array it is not refreshing the tableView, for example, you an observer or didSet
on the array.
Make sure you have the section available before trying to update
Tell the tableView you are going to make updates
let removeSection = 0
if tableView.numberOfSections >= removeSection+1 {
tableView.beginUpdates()
tableView.deleteSections([removeSection], with: .top)
tableView.endUpdates()
}
Upvotes: 1
Reputation: 285079
You have to create a specific IndexSet
instance from the Int
tableView.deleteSections(IndexSet(integer: 1), with: .top)
or use the init(arrayLiteral:
method of IndexPath
which infers an Int
array
tableView.deleteSections([1], with: .top)
Upvotes: 1
Reputation: 100503
You can try
tableView.deleteSections([0], with: .top)
change value to whatever index you need
Upvotes: 3