2017-07-02 78 views
0

我有一個數據源,其中包含像蘋果筆記應用程序中的不同列表。 當用戶點擊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) 
    } 
} 

那麼我怎麼能從這裏刪除項目正確的項目?還是有更好的方法,我不需要創建兩個數據源?

+0

在那裏產生的任何錯誤消息:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return showItems.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath as IndexPath) as! ItemCell let myItems = showItems[indexPath.row] as Items cell.myItems = myItems return cell } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCellEditingStyle.delete { showItems.remove(at: indexPath.row) let key = keyArray[indexPath.row] items.remove(at: key) keyArray.remove(at: indexPath.row) // Update the plist itemPlistUpdate() tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) } } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { editKey = keyArray[indexPath.row] } 

的editKey將在以下SEGUE方法中使用? –

+0

不,上面的代碼正常工作,因爲我還沒有從項目中刪除。 – Martin

+0

評論如何從這裏刪除正確的項目是什麼意思? 我可以看到你已經在刪除物品上面一行 –

回答

0

我創建的按鍵陣列解決了這個問題,該項目的編輯鍵將在稍後編輯。

var items = [Items]() 
var showItems = [Items]() 
var keyArray = [Int]() 
var editKey = Int() 

在填充showItems結構體時,每個項目都會在數組的幫助下獲得正確的鍵。

// Load the data from plist 
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[0] != "" { 
       items.append(Items(list: listItems[0], name: listItems[1], shop: listItems[2], price: listItems[3], remark: listItems[4])) 

       if listItems[0] == listName { 
        showItems.append(Items(list: listItems[0], name: listItems[1], shop: listItems[2], price: listItems[3], remark: listItems[4])) 

        keyArray.append(i) 
       } 
      } 
     } 
    } else { 
     print("Unable to get Plist") 
    } 
    tableView.reloadData() 
} 

因此表視圖將被showItems的內容填充。當涉及到刪除時,我使用相應的鍵從項目結構中刪除項目。

@IBAction func editItemDetail(segue: UIStoryboardSegue) { 
    if let editItemViewController = segue.source as? EditItemViewController { 

     // Save the edited item to the struct and update the plist 
     if let changedItem = editItemViewController.editedItem { 
      items.remove(at: editKey) 
      items.insert(changedItem, at: editKey) 
      // Update the plist 
      itemPlistUpdate() 
     } 
    } 
} 
0

您可以使用index(where:)找到項目對象的索引,然後將其刪除

下面示例代碼索引:

class Person { 
    var name: String 
    init(name: String) { 
     self.name = name 
    } 
} 


var people = [Person]() 
people.append(Person(name: "foo")) 
people.append(Person(name: "bar")) 
let toDelete = Person(name: "lorem") 
people.append(toDelete) 
people.append(Person(name: "sic")) 
people.append(Person(name: "quam")) 



let index = persons.index(where: {$0 === toDelete}) 
print(index) // print 2 
+0

好主意,但在這種情況下,我需要一個獨特的名稱或密鑰 - 我還沒有。我認爲這不是一種「安全」的方式。 – Martin

+0

我已經用示例代碼更新了答案 – PGLongo

+0

好的,謝謝。我會試試這個.. – Martin