0

I am trying to delete a row in a tableview that I am presenting using an array of objects. I have added a remove button at the corner and on the click it should remove the selected row but instead it always removes the first row no matter which row I click at.

I declared a variable first: private var indexForCell = Int() then I am using tableview method didSelectRowAt like this:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        indexForCell = indexPath.row
    }

Then I use my button that I added for deletion:

    @IBAction func removePokemon(_ sender: UIButton) {
        favourites.remove(at: indexForCell)
        self.tableView.reloadData()
    }

This makes the row delete but always the first one. Whichever row I select, it removes the first row always. How can I stop this?

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Sabih
  • 19
  • 1
  • 5
  • See here how you can delete rows in table view. https://stackoverflow.com/a/52818024/7905888 – A. Amini Sep 01 '20 at 13:22
  • You should update your question to make it clear that you have a delete button on each table view cell and you want to be able to tap that button and have it delete that specific cell. If that's the case your approach of saving the cell index when the user selects a cell won't work, since you can tap a button on a cell without selecting the cell first. – Duncan C Sep 02 '20 at 12:52
  • When posting a question, you need to describe the situation in enough detail that your readers understand it completely. Your question is vague and incomplete as written. I will hold off on down-voting it, but it needs improvement. – Duncan C Sep 02 '20 at 12:53

1 Answers1

0

The line of code private var indexForCell = Int() is a bug waiting to happen. Int() resolves to zero, which stands for the first row. Thus if you never select a row, it will try to delete the first one.

Make the variable an Optional:

private var indexForCell = Int?

Then rewrite your delete function:

@IBAction func removePokemon(_ sender: UIButton) {
    guard let indexToRemove = indexForCell else {
        print("No index selected")
        return
    }
    favourites.remove(at: indexToRemove)
    self.tableView.reloadData()
}

But you don't tell us what your favourites.remove(at:) function does, nor do you tell us how you populate your table view, so it's hard to tell if there are other problems as well.

(BTW, there are better ways to delete an individual cell than reloading the whole table view, but let's focus on getting your current approach working for now.)

Edit:

If you have a delete button on each cell that you want to delete the item without selecting it, you need a way to figure out which cell contains the button. I wrote a table view extension that lets you figure that out. See this link: https://stackoverflow.com/a/42043277/205185

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • This is somewhat working! at least I am able to delete any rows now no matter what index it is at but there is a slight issue. I would have to select the row in order to delete it. When I click the button I made in my cell it prints "no index selected" but when I click the cell itself and then click the button it then removes the cell. I have to select the cell then click the button on the cell to remove it. I wanted it to directly delete on press of the button without selecting the row – Sabih Sep 01 '20 at 13:46
  • You have a delete button on each cell? If so, you need to figure out which cell contains the button in your action code. Use the button's frame.origin, converted to the table view's coordinates, to figure out which cell contains the button. I wrote a table view extension that lets you figure out which index path contains a view, and have posted it here on SO. I'll try to find the link for you. – Duncan C Sep 01 '20 at 14:12