Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取自定义单元格按钮操作中的节号_Ios_Uitableview_Swift - Fatal编程技术网

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

Ios 获取自定义单元格按钮操作中的节号,ios,uitableview,swift,Ios,Uitableview,Swift,我有一个tableView,在几个部分中动态填充了自定义单元格 在我的CustomCell类中,我在单元格中有一个自定义复选框按钮的@iAction。有没有办法在iAction函数中获取sender按钮的节号 比如: @IBAction func checkboxButton(sender: CheckBox) { //Change Button state if sender.isChecked == true { sender.isChecked = false }

我有一个tableView,在几个部分中动态填充了自定义单元格

在我的CustomCell类中,我在单元格中有一个自定义复选框按钮的@iAction。有没有办法在iAction函数中获取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 = ???????

}

通过计算发送方的位置并在
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
}
这就是苹果在配件样品中的做法。

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

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
您可以设置:

cell.button.tag = sectionIndex
因此:

func checkboxButton(sender: CheckBox) { ...
sender.tag将是sectionIndex


希望您能理解。

我最近提出了一个利用Segues的使用并在Swift 2.2和XCode8中实现的解决方案

我有一个自定义单元格,用于有两个标签和一个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时,我可以使用这个值动态访问数据源中的数组。如下

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字段为我们提供了访问数组中正确位置的特定节索引


希望这对某人有所帮助。

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

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

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

//添加函数

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

伟大的成功了!swift略有不同:在
cellforrowatinexpath
中:
cell.button.tag=indexPath.section
为什么一半代码是Objective-C,另一半是swift?不,这只是apple doc的一个快速复制/粘贴数据源方法。显然,C&p即使语言相同,也有错误。很好的一个!谢谢。如果只使用节而不使用行,则返回nil。
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)")
}