gpt4 book ai didi

ios - 当我已经更新数据时出现错误 "Invalid update: invalid number of rows"

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:11 31 4
gpt4 key购买 nike

我的代码是这样的:

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)
}

我知道如果我不更新数据源中的数据会导致问题,所以我在更新 TableView 中的数据之前添加了这两行。

self.listClasses.remove(at:indexPath.row)
self.listClasses?.write(toFile: path, atomically: false)

但是还是不行。我仍然收到此错误

'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).

有人知道为什么会这样吗?

下面是我的 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)
}

//更新现在我删除了“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)
}

但是还是不行。

最佳答案

你有:

tableView.reloadData()
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)

永远不要两者都做。只做其中之一。在这种情况下,最好只做:

self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)

关于ios - 当我已经更新数据时出现错误 "Invalid update: invalid number of rows",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005426/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com