Ios 如何从UITableView中识别UITableView单元格中多个按钮的单击-Swift 4

Ios 如何从UITableView中识别UITableView单元格中多个按钮的单击-Swift 4,ios,swift,uitableview,uibutton,Ios,Swift,Uitableview,Uibutton,我想实现UITableView,其中每个UITableViewCell中有3个按钮。我想对每个按钮执行不同的操作。如何确定按下了哪个按钮,然后获取所选单元格的对象(索引行) UIViewController func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let show=shows[indexPath.row] let cel

我想实现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

在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类中执行按钮操作,请参阅此

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

@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
  • 使用委托模式
  • 使用块作为回调,并在cellForRow方法中处理块执行
  • AddAddTarget(:)为按钮单击添加目标方法
  • 详细信息:

    //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){
    
      }
    }
    
  • 第一种方法是上述三种方法中最好的,在这种情况下,您需要创建一个委托,将您的用户操作从计算单元重定向到视图控制器。检查下面的代码示例
  • 第二种方法与第一种类似,它只是使用块而不是委托方法和协议重定向相同的方法调用
  • 第三种方法并不好,因为它与软件开发行业中的
    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){
    
      }
    }
    

    我选择了选项2