如何在Tableview单元格中使按钮工作

时间:2019-08-03 22:05:37

标签: swift uitableview

当我按下Tableview单元格中的按钮时,它什么也没做。但是当我按下单元格时,该按钮起作用了。如何在不先按单元格的情况下使按钮做出反应?

@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)

        })
    }
}

1 个答案:

答案 0 :(得分:0)

将此代码写入cellForRowAt方法

cell.LikePressed.addTarget(self, action: #selector(LikePressed(sender:)), for: .touchUpInside)