【发布时间】:2017-07-02 07:44:43
【问题描述】:
我有一个数据源,其中包含不同的列表,例如 apples notes 应用程序。 当用户点击 ListViewController(所有列表)中的一行时,会出现第二个 ViewController,它只保存该列表的数据。 整个数据源在一个plist中,key是列表名:
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()
}
所以我在这里所做的是将整个数据源加载到 items 结构中,并将表视图的数据加载到 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)
}
}
那么我怎样才能从这里的项目中删除正确的项目?或者有没有更好的方法让我不需要创建两个数据源?
【问题讨论】:
-
是否有任何错误信息产生?
-
不,上面的代码工作正常,因为我还没有从项目中删除。
-
评论如何从这里删除正确的项目是什么意思?我可以看到您已经在删除上面一行的项目
-
items 保存所有列表项(来自所有列表),showItems 保存当前列表的项。我正在从 showItems 中删除,这很容易,因为这是活动 TableViewController 中的列表。要从项目中删除正确的项目,我需要一个索引 ..
-
您的 Items 结构/类中是否有任何独特的东西,例如产品 ID 或其他东西?
标签: ios uitableview swift3