Dav*_*vid 4 uitableview ios swift
我的代码如下所示:
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)
}
Run Code Online (Sandbox Code Playgroud)
我知道,如果不更新数据源中的数据,将会引起问题,因此在更新TableView中的数据之前,我添加了这两行。
self.listClasses.remove(at:indexPath.row)
self.listClasses?.write(toFile: path, atomically: false)
Run Code Online (Sandbox Code Playgroud)
但这仍然行不通。我仍然收到此错误
'无效的更新:第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)
}
Run Code Online (Sandbox Code Playgroud)
//更新现在我删除了“ 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)
}
Run Code Online (Sandbox Code Playgroud)
但这仍然行不通。
你有:
tableView.reloadData()
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
Run Code Online (Sandbox Code Playgroud)
绝对不要两者都做。只是做一个或另一个。在这种情况下,最好执行以下操作:
self.tableView.deleteRows(at: IndexPaths as! [IndexPath], with: .fade)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7755 次 |
| 最近记录: |