将按钮添加到配件视图的按钮

问题描述 投票:-1回答:2

我创建了一个按钮,我已将其分配给我的accessoryView。我看到了创建的按钮,但当我按下它时,没有任何反应。谁能看到我做错了什么?

   let accessoryButton = UIButton(type: .roundedRect)
    accessoryButton.setTitle("message", for: .normal)
    accessoryButton.sizeToFit()

    accessoryButton.addTarget(self, action: #selector(goToMessaging(sender:)), for: UIControlEvents.touchUpInside)

    cell.accessoryView = accessoryButton as UIView



 @objc func goToMessaging(sender: UIButton){
    print("message him")
}
swift button tableview target accessoryview
2个回答
0
投票

试试这个

@objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //let cell = ...
        cell.accessoryType = UITableViewCellAccessoryType.detailButton
        cell.tintColor = .red
}

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {

        print("accessory Button Tapped = \(indexPath.row)")
}

-1
投票

可能是因为你将按钮投射到UIView?

accessoryButton as UIView

如果必须是UIView,您可以尝试添加tapView识别器到accessoryView。

© www.soinside.com 2019 - 2024. All rights reserved.