I have question about expandable table view by header. I made it for section zero and its working correctly but when I try to make it same thing for section one too it gives error which is
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (2), 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).
If I didn't trigger second header function, first one still working. When I clicked second header ( which is section one) it gives that error.
Here my header view code:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let button = UIButton(type: .system)
button.setTitle("Kapat", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .lightGray
button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)
return button
}
if section == 1 {
let button2 = UIButton(type: .system)
button2.setTitle("Kapat", for: .normal)
button2.setTitleColor(.black, for: .normal)
button2.backgroundColor = .lightGray
button2.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button2.addTarget(self, action: #selector(handleExpandCloseForTeslim(sender:)), for: .touchUpInside)
return button2
}
else{
return nil
}
}
Here my action functions:
@objc func handleExpandCloseForAlim(sender: UIButton) {
var indexPaths = [IndexPath]()
for row in self.userAdressDict.indices{
let indexPath = IndexPath(row: row, section: 0)
indexPaths.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 0)
let adresso = self.userAdressDict[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPaths, with: .fade)
}else {
tableView.insertRows(at: indexPaths, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
@objc func handleExpandCloseForTeslim(sender: UIButton) {
var indexPathsForTeslim = [IndexPath]()
for row in self.userAdressDictForTeslim.indices{
let indexPath = IndexPath(row: row, section: 1)
indexPathsForTeslim.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 1)
let adresso = self.userAdressDictForTeslim[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPathsForTeslim, with: .fade)
}else {
tableView.insertRows(at: indexPathsForTeslim, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
And here my numberOfRowsInSection part:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
guard let element = self.userAdressDict.first as? adresStruct else { return 0 }
if element.isExpanded {
return self.userAdressDict.count
}
else {
return 0
}
}
if section == 1 {
guard let teslim = self.userAdressDictForTeslim.last as? adresStruct else { return 0 }
if teslim.isExpanded {
return self.userAdressDictForTeslim.count
}
else {
return 0
}
}
return 1
}
I can't find any solution. I assume sender thing working wrongly but it may be another problem. I hope I asked clearly.Please help me.