我有一个数据源,其中包含像苹果笔记应用程序中的不同列表。 当用户点击ListViewController(所有列表)中的一行时,会显示第二个ViewController,它只包含此列表的数据。 整个数据源是在plist中,关键是列表名称:从数据源中删除表格视图行数据
var items = [Items]()
var showItems = [Items]()
func loadPlistData() {
// Connect to plist and get the data
if let plist = PlistHandler(name: "Items") {
getPlist = plist.getMutablePlistDict()!
// Load the items into the table view data source
for i in 0..<getPlist.count {
listItems = (getPlist.object(forKey: "Item\(i)") as! NSArray) as! [String]
if listItems[1] != "" {
items.append(Items(list: listItems[0], name: listItems[1], shop: listItems[2], price: listItems[3]))
if listItems[0] == listName {
showItems.append(Items(list: listItems[0], name: listItems[1], shop: listItems[2], price: listItems[3]))
}
}
}
} else {
print("Unable to get Plist")
}
tableView.reloadData()
}
所以我在做什么这里是整个数据源加载到项目结构和数据表视图进入showItems结构。 当我添加一个新的项目,这将被添加到这两个结构工作正常。但是,当涉及到删除我遇到了问题:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
showItems.remove(at: indexPath.row)
// Ho to delete the correct item from items here?
// Update the plist
itemPlistUpdate()
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
}
}
那么我怎么能从这里删除项目正确的项目?还是有更好的方法,我不需要创建两个数据源?
在那里产生的任何错误消息:
的editKey将在以下SEGUE方法中使用? –
不,上面的代码正常工作,因为我还没有从项目中删除。 – Martin
评论如何从这里删除正确的项目是什么意思? 我可以看到你已经在删除物品上面一行 –