2017-07-02 105 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