delete row UITableView index issue

前端 未结 11 1713
遥遥无期
遥遥无期 2021-02-13 04:24

I\'m using the code below to delete a row in my tableview. First I delete the object from my array and then from the tableview using this code:

let i = IndexPath         


        
11条回答
  •  孤城傲影
    2021-02-13 04:27

    Smart solution to delete a row when a button is pressed in the corresponding cell.

    • In the custom cell class declare a callback variable

        var callback : ((UITableViewCell)->())?
      
    • In the custom cell class implement an IBAction and connect the button to that action

        @IBAction func buttonPressed(_ sender : UIButton) {
           callback?(self)
        }
      
    • In cellForRowAtIndexPath assign the closure containing the code to delete the cell

        cell.callback = { currentCell in
            let actualIndexPath = tableView.indexPath(for: currentCell)!
            self.myArray.remove(at: actualIndexPath.row)
            tableView.deleteRows(at: [actualIndexPath], with: .left)
        }
      

提交回复
热议问题