my code looks like this:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let IndexPaths = NSArray(array:[indexPath])
let plistPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = plistPath.appending("/ClassA.plist")
self.listClasses.remove(at:indexPath.row)
self.listClasses?.write(toFile: path, atomically: false)
tableView.reloadData()
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
}
I know that if I did not update the data from my data source it would cause problems, so I added these two lines before I update my data in the TableView.
self.listClasses.remove(at:indexPath.row)
self.listClasses?.write(toFile: path, atomically: false)
But it still doesn't work. I still received this error
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
Does anyone know why that happened?
Below is my code for TableView
@IBAction func Edit(_ sender: Any) {
setEditing(true, animated: true)
}
func tableView(_ tableView:UITableView, numberOfRowsInSection selection:Int) -> Int {
return self.listClasses.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath as IndexPath)
let row = indexPath.row
let rowDict = self.listClasses[row] as! NSDictionary
cell.textLabel?.text = rowDict["name"] as? String
return cell
}
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
let plistPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = plistPath.appending("/ClassA.plist")
let fileManager = FileManager.default
if (!(fileManager.fileExists(atPath: path)))
{
let bundle : String = Bundle.main.path(forResource: "ClassA", ofType: "plist")!
do {
try fileManager.copyItem(atPath: bundle as String, toPath: path)
}
catch {
NSLog("Error!")
}
}
self.listClasses = NSMutableArray(contentsOfFile:path)
//set up the textfield
self.txtField.isHidden = true
self.txtField.delegate = self
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.setEditing(editing, animated:true)
}
//update Now I deleted "tableView.reloadData()"
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let IndexPaths = NSArray(array:[indexPath])
let plistPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = plistPath.appending("/ClassA.plist")
self.listClasses.remove(at:indexPath.row)
self.listClasses?.write(toFile: path, atomically: false)
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
}
But it still doesn't work.
You have:
tableView.reloadData()
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
Never do both. Just do one or the other. In this case, preferably just do:
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
extension UITableView {
var dataHasChanged: Bool {
guard let dataSource = dataSource else { return false }
let sections = dataSource.numberOfSections?(in: self) ?? 0
if numberOfSections != sections { return true }
for section in 0..<sections {
if numberOfRows(inSection: section) != dataSource.tableView(self, numberOfRowsInSection: section) {
return true
}
}
return false
}
}
Usage:
if tableView.dataHasChanged {
tableView.reloadData()
} else {
// Do anything what you want here
// tableView.deleteRows(at: [indexPath], with: .none)
// tableView.reloadRows(at: [indexPath], with: .none)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With