2017-02-19 61 views
1

我可以從未經過濾的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

你需要顯示更多的代碼;特別是你的'cellForRowAt'和'numberOfRowsInSection',但是我的猜測是你並沒有從過濾的結果中刪除數據,因爲數組中的行數和預期的數量之間存在差異,因爲我按照異常消息 – Paulw11

+0

現在添加了代碼片段。你的意思是這些條目也應該從過濾的數組中刪除? – mostworld77

+0

是的,因爲'nameFiltered.count'不會反映已刪除的項目。 – Paulw11

回答

-1

刪除行後,加入self.tableView.reloadData()

+0

爲什麼downvote? –