delete row UITableView index issue

前端 未结 11 1709
遥遥无期
遥遥无期 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)
        }
      
    0 讨论(0)
  • 2021-02-13 04:27

    I use the following code in my applications:

    public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
    
        if editingStyle == UITableViewCellEditingStyle.delete
        {
            myArray.remove(at: indexPath.row)
            myArray2.remove(at: indexPath.row)
            // you would also save the new array here so that the next time you open the tableview viewController it shows the changes made.
    
            mytableview.reloadData()
    
        }
    }
    
    0 讨论(0)
  • 2021-02-13 04:31

    I'm not sure about from where you get your var rowNum, but to me this issue looks like something similar it already happened to me.

    For me the problem was that passing to the cell the indexPath in the tableView: cellForRowAt indexPath: was causing this value to be not updated when a cell was deleted.

    in other words, all the buttons that are ordered after the button of the deleted cell must have their index shifted by -1, or they will delete the row n+1.

    row A index 0
    row B index 1
    row C index 2
    row D index 3
    

    deleting B it happens

    row A index 0
    row C index 2
    row D index 3
    

    while C should have index 1 and D should have index 2. In this case tapping the button to delete C will cause D to be deleted, and tapping the button to delete D will cause a crash.

    I hope I was clear enough.

    0 讨论(0)
  • 2021-02-13 04:34

    After delete the row, reload particular row

    let indexPath = IndexPath(item: rowNum, section: 0)
    array.remove(at: rowNum)
    tableView.beginUpdates()
    myTableView.deleteRows(at: [i], with: UITableViewRowAnimation.automatic)
    tableView.endUpdates()
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
    
    0 讨论(0)
  • 2021-02-13 04:35

    Try this!

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
    
            //1. remove data from model
            data.remove(at: indexPath.row)
    
            //2. remove row from view
            tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)
        }
    }
    
    0 讨论(0)
  • 2021-02-13 04:36

    I think you store rowNum variable with your cell and it's set in cellForRow(at:) method. If what I think is right then here's a thing.

    UITableView try to do at least work as possible. This means that after you delete a cell, UITableView won't gonna call cellForRow(at:) methods on its datasource again until you call reloadData() or reloadRows(at:animation:) methods.

    So when you delete the first cell, rowNum variable on other cells won't gonna be updated until you call reloadData() as you tried and saw that's work.

    My suggestion is don't keep rowNum variable at a cell but ask UITableView for the index path of any cell via indexPath(for:) method on UITableView instead.

    0 讨论(0)
提交回复
热议问题