【问题标题】:Adding target to button to accessory View将目标添加到附件视图的按钮
【发布时间】:2018-04-05 21:32:45
【问题描述】:

我创建了一个分配给我的附件视图的按钮。我可以看到创建的按钮,但是当我按下它时,什么也没有发生。谁能看到我做错了什么?

   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


    【解决方案1】:

    试试这个

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

    【讨论】:

      【解决方案2】:

      可能是因为您将按钮转换为 UIView 吗?

      accessoryButton as UIView
      

      如果它必须是 UIView,您可以尝试将轻击手势识别器添加到附件视图。

      【讨论】:

      • 不,这不起作用。我尝试使用手势识别器将它同时转换为按钮和 UIView,但没有成功
      • 你在哪里初始化按钮?看起来您正在 cellForRowAtIndexPath 中创建按钮。
      • 是的,这就是我创建它的地方
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多