1

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 enable delete button if post == username like this inside cellForRowAt indexPath: IndexPath function:

if postsArray[indexPath.row].username == currentUser.generalDetails.userName{
            cell.deletePostButton.isHidden = false
        }else{
            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){


FIRDatabase.database().reference().child("posts").child(postsArray[indexPath.row].postId).removeValue()
}

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?

AL.
  • 36,815
  • 10
  • 142
  • 281
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92

1 Answers1

1

You are right with your approach:

FIRDatabase.database().reference().child(postsArray[indexPath.row].postId).removeValue()

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

FIRDatabase.database().reference().child("posts").child(postsArray[indexPath.row].postId).removeValue()

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.

Community
  • 1
  • 1
ronatory
  • 7,156
  • 4
  • 29
  • 49
  • Thanks for the quick reply but can you check my edit. The problem is that I have to declare indexPath outside of the tableview function and call it inside tableview :) And the keyvalue is the postsArray[indexPath.row].postId :) So everything else should be OK – Tarvo Mäesepp Oct 13 '16 at 11:11
  • So you are sure everytime that you use section 0 and row 1 for the delete? – ronatory Oct 13 '16 at 11:15
  • Ohh. no no. I am struggling with this part also. Do not know how to declare the indexPath where the button is in. – Tarvo Mäesepp Oct 13 '16 at 11:16
  • k, maybe you can add the part with the button. It should be quite easy when you just want to delete the item on the selected row, but if you are doing something special with a extra button, then this information is maybe useful in the question. Or is it like that, that you just select a row and then something like an alert view/ action sheet appears and you delete the post by pressing the button? – ronatory Oct 13 '16 at 11:21
  • Actually I do not prompt any modal but for now just want to press button and delete the row and info from Firebase. Check the photo – Tarvo Mäesepp Oct 13 '16 at 11:27
  • huhh.. What the hecking hack, it is working(printing the row). And I got it deleding. I had the post.key inside my struct. Thank you very much for the help :) You are hero. Do not know where did you take that logic. Can you explain little the sender.tag / 100? – Tarvo Mäesepp Oct 13 '16 at 13:19
  • ok, I first just copy and paste the code and took a short test with 10 items in a tableView. So in the end this "hacky" way work only for a special case. e.g. you will have 100 entries in your table view, the current solution will not work. So if you are sure you have only 1 section, than I recommend you the updated version. just set your section to 0 and your row to your sender.tag. probably the thing with / and % was just a special case in the other question – ronatory Oct 13 '16 at 13:49