【问题标题】:Deleting table view row data from data source从数据源中删除表视图行数据
【发布时间】: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


【解决方案1】:

您可以使用index(where:)在items中查找对象的索引,然后将其删除

这里是一个带索引的示例代码:

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

【讨论】:

  • 好主意,但在这种情况下,我需要一个唯一的名称或密钥——我还没有。我认为这不是一种“安全”的方式。
【解决方案2】:

我通过为稍后将要编辑的项目创建一个键数组和一个 editKey 解决了这个问题。

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 的内容填充。在删除时,我使用相应的键从 items 结构中删除项目。

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方法:

@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()
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多