【发布时间】:2016-12-06 21:40:40
【问题描述】:
我的代码如下所示:
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)
}
但还是不行。
【问题讨论】:
-
either call
reloadDataor calldeleteRowsnot both. -
@Paulw11 我试图删除其中一个(两种方式),但它仍然不起作用。
-
不,这是对所示代码的修复;您是否在其他地方删除该行?如果您删除
deleteRows,留下reloadData并且您仍然收到该消息,那么您将删除其他地方,因为reloadData永远不会导致该异常
标签: ios swift uitableview