【问题标题】:How do I make button in tableview cell work [duplicate]如何使表格视图单元格中的按钮工作[重复]
【发布时间】:2019-08-03 22:05:37
【问题描述】:

当我按下 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)

        })
    }
}

【问题讨论】:

  • 您的单元格是动态的吗?如果是这样,您必须使用静态单元格才能使其上的按钮起作用。
  • "当我按下 Tableview 单元格中的按钮时,它什么也不做" 如果您为自己编写代码,“if let index = MyTable.indexPathForSelectedRow {”会告诉您什么?
  • 我的单元格是自定义单元格,当我使用动态单元格时它会出错,因为我使用的是视图控制器而不是表格控制器
  • "if let index = MyTable.indexPathForSelectedRow" 告诉我单元格的 indexpath.row(包含 postID 和 uid)

标签: swift uitableview


【解决方案1】:

在 cellForRowAt 方法中编写此代码

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

【讨论】:

  • 谢谢,但这对我不起作用,我需要在点击按钮以使我的功能工作时获取 indexpath.row
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多