通过从筛选的搜索结果中滑动来快速删除条目

时间:2017-02-19 10:43:02

标签: ios swift xcode uisearchcontroller

我可以删除未经过滤的tableview中的条目, 但是使用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!
}

1 个答案:

答案 0 :(得分:-1)

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