Tarvo Mäesepp Tarvo Mäesepp - 3 months ago 61
Swift Question

Remove current cell value from Firebase

First of all, I didn't see any question related to this. I am trying to delete specific value from Firebase which value is got from

tableview cell

First I
delete button if
post == username
like this inside
cellForRowAt indexPath: IndexPath

if postsArray[indexPath.row].username == currentUser.generalDetails.userName{
cell.deletePostButton.isHidden = false
cell.deletePostButton.isHidden = true

And here I call deletePost() function:

cell.deletePostButton.addTarget(self, action: #selector(deletePost), for: .touchUpInside)

And I try to run this function but it terminates the app:

func deletePost(sender: UIButton, cellForRowAt indexPath: IndexPath){


To illustrate this, that is my tableview with the delete button and the delete button(inside red thing) should delete the row from Firebase:

enter image description here

Why the function terminates the app?


You are right with your approach:


But if you have a parent node posts you have to use this:


sry didn't used the key value KTxXAp4o... from your screenshot, will have a look again

Update regarding to updates of question:

The only way I figured out to access the indexPath without adding an additional parameter is this kind of hacky way regarding to this answer:

So update your deletePost function to this for accessing the specific row:

  func deletePost(sender: UIButton){
    // If you have only one section
    let section = 0
    let row = sender.tag
    let indexPath = IndexPath(row: row, section: section)

    print("indexPath row: \(indexPath.row)")

And before your call to the deletePost() function, add the tag to the button:

// add the row as the tag
cell.deleteButton.tag = indexPath.row
// still the same    
cell.deleteButton.addTarget(self, action: #selector(deletePost), for: .touchUpInside)

After this you have access to the specific row and your app should not terminate.