SwipeCellKit:为什么从列表中删除一个项目,不更新 UITableview?

SwipeCellKit: Why removing an item from list, doesn't update the UITableview?

当我使用 SwipeTableViewCell 从列表中删除一个项目时,表格视图没有得到更新。 滑动有效,我也可以点击单元格后面的删除按钮,但是一旦点击它,它只会隐藏删除按钮但不会更新表格视图。 这是我的代码的样子:


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return carList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_id", for: indexPath) as! CarCell

        (cell as SwipeTableViewCell).delegate = self
        
        let car = carList[indexPath.row]
        cell.configure(with: car)

        return cell
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        
        guard orientation == .right else { return nil }
        
        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            
            self.carList.remove(at: indexPath.row)
        }
        deleteAction.hidesWhenSelected = true

        return [deleteAction]
    }


当我调用tableview的reloadData时它起作用了,但我猜这不是正确的方法。以下代码片段有效:


    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        
        guard orientation == .right else { return nil }
        
        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            
            self.carList.remove(at: indexPath.row)
            
            self.tableView.reloadData() // <-- this works! But without animation
        }
        deleteAction.hidesWhenSelected = true

        return [deleteAction]
    }


您必须添加代码来更新视图,而不是重新加载整个 table 视图只需 删除

let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
        
    self.carList.remove(at: indexPath.row)
    self.tableView.deleteRows(at: [indexPath], with: .fade)
}