【问题标题】:I have multiple section , each cell has a button how to know indexpath.row and indexpath.section if user clicks a button [duplicate]我有多个部分,每个单元格都有一个按钮,如果用户单击按钮,如何知道 indexpath.row 和 indexpath.section [重复]
【发布时间】:2020-10-09 06:40:52
【问题描述】:

使用此代码,我只获得具有 indexpath.row 的标签,如何从中制作按钮事件。

TaskPlayButton 是我传递 indexpath.row 的按钮 对于单个部分,现在如何在多个部分上执行此操作。

有没有其他方法知道点击了哪个按钮?

    func numberOfSections(in tableView: UITableView) -> Int {
        return mobileBrand.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mobileBrand[section].modelName?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell

        cell.TaskName.text = mobileBrand[indexPath.section].modelName?[indexPath.row]

        cell.TaskPlayButton.tag = indexPath.row

        cell.TaskPlayButton.addTarget(self, action: #selector(PlayBtnClicked( _:)), for: UIButton.Event.touchUpInside)
         return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
        view.backgroundColor = UIColor.rgb(red: 245, green: 245, blue: 245)

        let lbl = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
        lbl.font = UIFont.systemFont(ofSize: 20)
        lbl.text = mobileBrand[section].brandName
        view.addSubview(lbl)



        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndex == indexPath.row && isCollapse == true && selectedSection == indexPath.section{
            return 240

        }else{
            return 60
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if selectedIndex == indexPath.row && selectedSection == indexPath.section{
            if self.isCollapse == false
            {
                self.isCollapse = true
            }else{
                self.isCollapse = false
            }
        }else{
            self.isCollapse = true
        }
        self.selectedIndex = indexPath.row
        self.selectedSection = indexPath.section
        TaskTableView.beginUpdates()
        TaskTableView.endUpdates()
    }

    @objc func PlayBtnClicked(_ sender : UIButton)
    {
        print(sender.tag)
    }

【问题讨论】:

    标签: swift uitableview swift5


    【解决方案1】:

    你可以使用闭包,请看我的代码:

    class YourTableViewCell: UITableViewCell {
    
        var button: UIButton!
    
        var buttonAction: ((UIButton) -> Void)?
    
        @objc func buttonPressed(_ sender : UIButton)) {
            self.buttonAction?(sender)
        }
    }
    

    在您的表格视图委托中实现它:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
    
        cell.TaskName.text = mobileBrand[indexPath.section].modelName?[indexPath.row]
        cell.TaskPlayButton.tag = indexPath.row
    
        cell.buttonAction = { sender in
            // do your action
            // in here, you can get indexPath.row and indexPath.section
        }
    
        cell.TaskPlayButton.addTarget(self, action: #selector(PlayBtnClicked( _:)), for: UIButton.Event.touchUpInside)
             return cell
    }
    

    希望这个答案可以帮助到你:)

    【讨论】:

      【解决方案2】:
      1. indexPath传递给单元格,点击该单元格中的按钮,可以得到当前的indexPath

      2. 自定义一个按钮持有一个属性:indexPath。将indexPath 传递给方法cellForRowAt 中的按钮。当您点击按钮时,您可以使用button.indexPath 来获得您想要的。

      【讨论】:

        【解决方案3】:

        您可以使用另一种方法,例如像这样将标签传递给按钮

        cell.button.tag = (indexpath.section * 1000) + indexpath.row
        

        现在在您的按钮操作上,您可以像

        let row = sender.tag%1000
        let section = sender/1000
        

        【讨论】:

          【解决方案4】:

          我只是给你快速提示。它只是帮助您处理项目中的所有表格视图。 您可以在这里关注我的回答 - https://stackoverflow.com/a/62481414/8135121

          这是给你的。 您可以通过按钮的位置来抓取它。 我给你解释一下。

          为您的班级制作一个通用的表格视图扩展。就用这个吧。

          extension UITableView {
              
              func getCellFrom(sender : UIView,completion : @escaping (UITableViewCell,IndexPath)->()){
                  var superview = sender.superview
                  while let view = superview, !(view is UITableViewCell) {
                      superview = view.superview
                  }
                  guard let cell = superview as? UITableViewCell else {
                      return
                  }
                  guard let indexPath = self.indexPath(for: cell) else {
                      return
                  }
                  completion(cell,indexPath)
              }
          }

          在您的单元格中为 IndexPath 函数中的行设置目标。

          cell.TaskPlayButton.addTarget(self, action: #selector(PlayBtnClicked( _:)), for: UIButton.Event.touchUpInside)

          在 PlayBtnClicked 操作方法中调用它。

          tableView.getCellFrom(sender: sender) { [weak self] (cell, indexPath) in
                // Just use indexPath in here
                //This closure return your cell and your button's indexPath also
             }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-17
            • 1970-01-01
            • 2022-06-15
            • 2014-01-03
            • 1970-01-01
            • 2014-06-14
            相关资源
            最近更新 更多