【问题标题】:Swift deleting entry with swipe from filtered search results从过滤的搜索结果中快速删除条目
【发布时间】:2017-02-19 10:43:02
【问题描述】:

我可以从未过滤的表格视图中删除条目, 但是从使用 SearchController 过滤的数据中我得到一个错误

reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (10)

我认为问题在于从过滤结果中获取 id?

我的代码是:

var NameFiltered = [String]()

func tableView(_ tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: IndexPath) {
            if (editingStyle == UITableViewCellEditingStyle.delete) {
                    let id = ItemIDList[indexPath.row]
                    ItemNameList.remove(at: indexPath.row)
                    ItemIDList.remove(at: indexPath.row)
                    let deleteSQL = "DELETE FROM item WHERE item_id = '\(id)'"
                    itemTable.deleteRows(at: [indexPath], with: .fade)
}
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(SearchBarActive){
        return NameFiltered.count
    }

    return itemIDList.count
}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"

    var cell = self.exerciseManageTable.dequeueReusableCell(withIdentifier: cellIdentifier)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellIdentifier)
    }

    let name_data = itemNameList[indexPath.row]

    let groupname_data = itemGroupNameList[indexPath.row]
    cell!.detailTextLabel?.text = "group: \(groupname_data)"

    if(SearchBarActive){
        cell!.textLabel?.text = NameFiltered[indexPath.row]
    }else{
        cell!.textLabel?.text = name_data
    }
    return cell!
}

【问题讨论】:

  • 你需要展示更多代码;特别是您的cellForRowAtnumberOfRowsInSection,但我的猜测是您没有从过滤结果中删除数据,因为数组中的行数与异常消息中预期的行数之间存在差异跨度>
  • 我现在添加了代码 sn-ps。你的意思是这些条目也应该从过滤后的数组中删除?
  • 是的,因为nameFiltered.count 不会反映已删除的项目。

标签: ios swift xcode uisearchcontroller


【解决方案1】:

删除行后,添加self.tableView.reloadData()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多