gpt4 book ai didi

ios - 从 Swift 3 中的自定义 UITableViewCell 检测 UIButton

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:45 24 4
gpt4 key购买 nike

<分区>

我有一个自定义的 UITableViewCell,我在其中使用 Interface Builder 连接了我的 UIButton

@IBOutlet var myButton: UIButton!

在 UITableViewController 的单元格配置下,我有以下代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var customCell = tableView.dequeueReusableCell(withIdentifier: self.MY_CELL_IDENTIFIER, for: indexPath) as! myCustomCell

// CONFIGURE OTHER CELL PARAMETERS

customCell.myButton.tag = indexPath.row

customCell.myButton.addTarget(self, action: #selector(myButtonPressed), for: UIControlEvents.touchUpInside)

return customCell

}

最后,我有

private func myButtonPressed(sender: UIButton!) {
let row = sender.tag
print("Button Sender row: \(row)")
}

此代码无效,除非我将函数定义更改为以下:

@objc private func myButtonPressed(sender: UIButton!) {
let row = sender.tag
print("Button Sender row: \(row)")

}

有没有更好的方法在 Swift 3 中的自定义 UITableViewCell 上实现 UIButton

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com