【问题标题】:How to identify click from multiple buttons in a UITableViewCell from a UITableView - Swift 4如何识别来自 UITableView 的 UITableViewCell 中多个按钮的点击 - Swift 4
【发布时间】:2019-07-30 01:59:26
【问题描述】:

我想实现 UITableView 我想在每个 UITableViewCell 中有 3 个按钮。我想为每个按钮执行不同的操作。如何识别按下哪个按钮,然后获取所选单元格的对象(索引行)?

UIViewController

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let show=shows[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "ShowCell") as!
    ShowCell

    cell.setShow(show: show)
    return cell
}

UITableViewCell

@IBOutlet weak var graphButton: FlatButton!
@IBOutlet weak var buyButton: FlatButton!
@IBOutlet weak var reviewButton: FlatButton!


func setShow(show :StubHubEvent ){


    let url = URL(string: show.imageurl)!

    showImageView.af_setImage(withURL: url)
    showImageView.contentMode = .scaleAspectFill
    showImageView.clipsToBounds = true
    nameLabel.text = show.title
    dateLabel.text = show.time

【问题讨论】:

    标签: ios swift uitableview uibutton


    【解决方案1】:

    在 UIviewcontroller 而不是 UITableViewCell 中实现您的按钮操作,在 cellforRow 内创建目标,并为每个目标添加标签以识别用户按下了哪个按钮。例如

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let show=shows[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ShowCell") as!
        ShowCell
    
         cell.graphButton.tag = indexPath.row
         cell.buyButton.tag = indexPath.row
         cell.reviewButton.tag = indexPath.row
    
         cell.graphButton?.addTarget(self, action: #selector(self.graphButtonClicked(_:)), for: .touchUpInside)
         cell.buyButton?.addTarget(self, action: #selector(self.buyButtonClicked(_:)), for: .touchUpInside)          
         cell.reviewButton?.addTarget(self, action: #selector(self.reviewButtonClicked(_:)), for: .touchUpInside)
    
    
        cell.setShow(show: show)
        return cell
    }
    

    并像处理操作一样

     @objc func buyButton( _ sender: UIButton) {
           print("buyButton Action Found the index of \(sender.tag)")
    
    }
    
    @objc func graphButtonClicked( _ sender: UIButton) {
           print("graphButtonClicked Action Found the index of \(sender.tag)")
    
    }
    
    @objc func reviewButtonClicked( _ sender: UIButton) {
           print("reviewButtonClicked Action Found the index of \(sender.tag)")
    
    }
    

    选项 2

    如果您想在 UItableviewcell 类中使用委托模式执行按钮操作,请参考此duplicate answer

    【讨论】:

    • 我选择了选项 2
    【解决方案2】:

    有两种方法可以从 TableViewCell 中获取 ViewController 中的按钮单击执行

    1. 使用委托模式
    2. 使用块作为回调并在 cellForRow 方法中处理块执行
    3. 添加 addTarget(:) 为按钮单击添加目标方法

    详情:

    1. 第一种方法是上述三种方法中最好的,在这种方法中,您需要创建一个委托,将您的用户操作从单元格重定向到视图控制器。检查下面的代码示例。
    2. 第二种方法与第一种方法类似,只是使用块而不是委托方法和协议来重定向相​​同的方法调用。
    3. 第三种方法不好,因为它与软件开发行业中的indexPath.row 值紧密耦合

    内聚应该高,耦合应该低。

    第一种方法的代码:

    //MARK:- Model - StubHubEvent
    class StubHubEvent {
      //you model class implementation
    }
    
    //MARK:- Protocol - ShowCellUIInteractionDelegate - used to redirect user actions from cell to viewController
    protocol ShowCellUIInteractionDelegate: AnyObject {
      func showCell(cell: ShowCell, didTapBuyFor show: StubHubEvent)
      func showCell(cell: ShowCell, didTapGraphFor show: StubHubEvent)
      func showCell(cell: ShowCell, didTapReviewFor show: StubHubEvent)
    }
    
    //MARK:- Cell-  ShowCell
    class ShowCell: UITableViewCell {
    
      var show: StubHubEvent!
      weak var delegateUIInteraction: ShowCellUIInteractionDelegate?
    
    
      func setShow(show :StubHubEvent ){
        self.show = show
        //your other setup
      }
    
      //Bind these three action from cell to buttons as a .touchUpInside event
      @IBAction func buttonBuyDidTap( _ sender: UIButton) {
        self.delegateUIInteraction?.showCell(cell: self, didTapBuyFor: self.show)
      }
    
      @IBAction func buttonGraphDidTap( _ sender: UIButton) {
        self.delegateUIInteraction?.showCell(cell: self, didTapGraphFor: self.show)
      }
    
      @IBAction func buttonReviewDidTap( _ sender: UIButton) {
        self.delegateUIInteraction?.showCell(cell: self, didTapReviewFor: self.show)
      }
    }
    
    //MARK:- ViewController - ShowListingViewController
    class ShowListingViewController: UIViewController {
      //you ShowListingViewController implementation
    }
    //MARK:- Extension - ShowCellUIInteractionDelegate
    extension ShowListingViewController: ShowCellUIInteractionDelegate {
      //execute your logic for the show model object
      func showCell(cell: ShowCell, didTapBuyFor show: StubHubEvent){
    
      }
      func showCell(cell: ShowCell, didTapGraphFor show: StubHubEvent){
    
      }
      func showCell(cell: ShowCell, didTapReviewFor show: StubHubEvent){
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多