swift “获取自定义单元格中的节号”按钮操作

g2ieeal7  于 2022-10-31  发布在  Swift
关注(0)|答案(6)|浏览(330)

我有一个tableView,它在几个部分中动态填充了自定义单元格。
在我的CustomCell类中,我有一个@IBAction,用于单元格中的一个自定义复选框按钮。有没有办法在IBAction函数中获取sender按钮的节号?
类似于:

@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 = ???????

}
ujv3wf0j

ujv3wf0j1#

通过计算发送方的位置并在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

mepcadol

mepcadol2#

在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
}
shyt4zoc

shyt4zoc3#

用于在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)")
}
pes8fvy9

pes8fvy94#

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

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

您可以设置:

cell.button.tag = sectionIndex

所以在:

func checkboxButton(sender: CheckBox) { ...

sender.tag将是sectionIndex
希望你明白。

u0njafvf

u0njafvf5#

我最近提出了一个解决这个问题的解决方案,它利用了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字段,我使用该字段保存节索引。

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"{

       ...

    }
}

这里的sender是按钮,因此.tag字段为我们提供了特定的节索引,以便访问数组中的正确位置
希望这能帮助到一些人。

snz8szmq

snz8szmq6#

''-在tableview中获取节和行索引的方法之一按钮是:-

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

相关问题