delete row UITableView index issue

前端 未结 11 1708
遥遥无期
遥遥无期 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:41

    Problem

    The problem in you case is the Tag you are setting on the Button , button Tag is not changed when you are deleting the Rows , as its the All Cell is not Reloaded. i guess you are setting tag in cellforRowIndexpath you try with putting tag on button in tableWillDisplayCell

    or

    the best way to do this is below .

    Solution

    you can get the IndexPath in other way

    Suppose if your view hierarchy is

    UITableViewCell-> contentView -> button

    in you button click method

     if let cell = sender.superview?.superview as? UITableviewCell{
            let indexPath = myTableView.indexPath(for: cell)
        // Now delete the table cell
        myArray.remove(at: indexPath.row)
        myTableView.beginUpdates()
        myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
        myTableView.endUpdates()
    }
    

    i hope this will work for you .

提交回复
热议问题