我有一個數據源,其中包含像蘋果筆記應用程序中的不同列表。 當用戶點擊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
評論如何從這裏刪除正確的項目是什麼意思? 我可以看到你已經在刪除物品上面一行 –