【问题标题】:Get section number in custom cell button action在自定义单元格按钮操作中获取节号
【发布时间】:2015-01-11 16:08:53
【问题描述】:

我有一个 tableView 动态填充了几个部分的自定义单元格。

在我的 CustomCell 类中,我有一个 @IBAction 用于单元格中的自定义复选框按钮。有没有办法在IBAction函数中获取发送者按钮的段号?

类似:

@IBAction func checkboxButton(sender: CheckBox) {
    //Change Button state
    if sender.isChecked == true { sender.isChecked = false }
    else { sender.isChecked = true }

    //Get section number of clicked button
    var sectionNumber = ???????

}

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    您可以通过计算发件人的位置并在UITableView中找到该位置的行来轻松找到行

    var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    if let indexPath = self.tableView.indexPathForRowAtPoint(position)
    {
        let section = indexPath.section
        let row = indexPath.row
    }
    

    这就是 Apple 在附件示例中的做法。 https://developer.apple.com/library/ios/samplecode/Accessory/Listings/AccessoryViewController_m.html#//apple_ref/doc/uid/DTS40008066-AccessoryViewController_m-DontLinkElementID_4

    【讨论】:

    • 不错的一个!谢谢。
    【解决方案2】:

    在 Swift 3.0 中,有些东西略有变化,例如 'CGPointZero' - > 'CGPoint.zero' & 'indexPathForRowAtPoint' -> 'indexPathForRow(at:position)' 等,因此您可以从这段代码中获取点击按钮的真实行和部分:

    let position: CGPoint = sender.convert(CGPoint.zero, to: self.tableView)
    if let indexPath = self.tableView.indexPathForRow(at: position)
    {
      let section = indexPath.section
      let row = indexPath.row
    }
    

    【讨论】:

    • 如果你只使用没有行的部分,它返回 nil。
    【解决方案3】:

    对于调用按钮时在 Swift 4 和 Swift 5 中查找 indexPath,

    // 首先你在“cellForRowAt”中的按钮上添加目标

    cell.myBtn.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
    

    // 添加函数

    func btnAction(_ sender: UIButton) {
        let point = sender.convert(CGPoint.zero, to: yourTableView as UIView)
        let indexPath: IndexPath! = yourTableView.indexPathForRow(at: point)
        print("indexPath.row is = \(indexPath.row) && indexPath.section is = \(indexPath.section)")
    }
    

    【讨论】:

      【解决方案4】:

      其中一种方法,假设您在一个单元格中有自定义单元格和按钮... 通常的模式是: (如果您使用带有数据源的表格视图)在内部配置单元格时:

      - (UITableViewCell *)tableView:(UITableView *)tableView
           cellForRowAtIndexPath:(NSIndexPath *)indexPath
      

      你可以设置:

      cell.button.tag = sectionIndex
      

      所以在:

      func checkboxButton(sender: CheckBox) { ...
      

      sender.tag 将是 sectionIndex

      希望你明白了。

      【讨论】:

      • 太棒了!那成功了! swift中略有不同:在cellForRowAtIndexPathcell.button.tag = indexPath.section
      • 为什么一半的代码是Objective-C而另一个是Swift?
      • 不,这只是来自苹果文档的快速复制/粘贴数据源方法。
      • 显然 c&p 因为即使语言相同,它也有错误。
      【解决方案5】:

      我最近利用 Segues 的使用来解决这个问题,并在 Swift 2.2 和 XCode8 中实现它。

      我有一个自定义单元格,用于具有 2 个标签和一个 UIButton 的标题。 我所有的数据都在一个数组中,我想动态访问这些数据。每个部分都是一个部分标题的数组,并且在该类中,这些部分包含其他数组以完成生成我的 tableView。我的目标是根据自定义单元格(标题)中的按钮单击来获取该部分的索引,并在不同的视图中更改数据。

      使用我使用的自定义标题单元格填充 tableView:

      override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let myCell = tableView.dequeueReusableCellWithIdentifier("REUSABLE ID")! as! MyHeaderCell
          let team = array[section]
          myCell.teamName.text = team.name
          myCell.projectName.text = team.project
          myCell.addMemberButton.tag = section        
          return myCell
      }
      

      使用情节提要,MyHeaderCell 中按钮 (addMemberButton) 的 IBOutlet 具有我用来保存节索引的 .tag 字段。因此,当我准备 segue 时,我可以使用这个值来动态访问 DataSource 中的数组。如下

      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          if segue.identifier == "segue1" {
      
              ...
      
          }else if segue.identifier == "AddMemberSegue"{
              let ix = sender?.tag
              let destVC = segue.destinationViewController as! AddMemberViewController
              self.myArray = array[ix!]
              destVC.delegate = self
      
          }else if segue.identifier == "segue3"{
      
             ...
      
          }
      } 
      

      这里的发送者是按钮,所以 .tag 字段为我们提供了特定的部分索引以访问数组中的正确位置

      希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多