0

When I press the button within my Tableview cell it does nothing. But when I press the cell the button works. How do I make the button react without pressing cell first?

@IBAction func LikePressed(_ sender: Selector) {
    let ref = Database.database().reference()

    if let index = MyTable.indexPathForSelectedRow {
        let aaa = timeLike[index.row].like
        let bbb = timeLike[index.row].id
        ref.child("users").child(bbb).child("timeline").child(aaa).child("likes").runTransactionBlock({
            (currentData:MutableData!) in
            var value = currentData.value as? Int
            //check to see if the likes node exists, if not give value of 0.
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return TransactionResult.success(withValue: currentData)

        })
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Peakz
  • 13
  • 4
  • Is your cell dynamic? If so you must use static cells in order to make the button on it work. – Muhammed Gül Aug 03 '19 at 22:08
  • "when i press the button within my Tableview cell it does nothing" If you wrote code for yourself, what does "if let index = MyTable.indexPathForSelectedRow {" tell you? – El Tomato Aug 03 '19 at 22:14
  • My cell is a custom cell, when i use dynamic cell it gives error because I'm using a view controller not not table controller – Peakz Aug 03 '19 at 22:32
  • "if let index = MyTable.indexPathForSelectedRow" tells me the indexpath.row(holds postID and uid) of the cell – Peakz Aug 03 '19 at 22:34

1 Answers1

0

Write this code in cellForRowAt method

cell.LikePressed.addTarget(self, action: #selector(LikePressed(sender:)), for: .touchUpInside)
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Khyati Modi
  • 630
  • 1
  • 9
  • 19
  • Thank you but this didn't work for me, i need get indexpath.row as i tap the the button for my function to work – Peakz Aug 06 '19 at 12:23