有错误"无效更新:行数无效"当我已经更新数据

时间:2016-12-06 21:40:40

标签: ios swift uitableview

我的代码如下:

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)

但它仍然无法奏效。我仍然收到此错误

  

'无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(6)必须等于更新前该部分中包含的行数(6)加上或减去从该部分插入或删除的行数(0插入,1删除)和加或减移入或移出该部分的行数(0移入,0移出)。

有谁知道为什么会这样?

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

但它仍然无效。

2 个答案:

答案 0 :(得分:4)

你有:

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

永远不要两者兼得。只做一个或另一个。在这种情况下,最好只做:

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

答案 1 :(得分:1)

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

用法:

 if tableView.dataHasChanged {
     tableView.reloadData()
 } else {
     // Do anything what you want here
     // tableView.deleteRows(at: [indexPath], with: .none)
     // tableView.reloadRows(at: [indexPath], with: .none)
 }