我有一个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 = ???????
}
6条答案
按热度按时间ujv3wf0j1#
通过计算发送方的位置并在
UITableView
中找到该位置处的行,您可以轻松地找到该行这是Apple在“配件”示例中的做法。https://developer.apple.com/library/ios/samplecode/Accessory/Listings/AccessoryViewController_m.html#//apple_ref/doc/uid/DTS40008066-AccessoryViewController_m-DontLinkElementID_4
mepcadol2#
在Swift 3.0中,有些东西稍微做了一些改变,比如
'CGPointZero' - > 'CGPoint.zero' & 'indexPathForRowAtPoint' -> 'indexPathForRow(at:position)'
等,这样你就可以从下面的代码中得到点击按钮的真实的行和部分:shyt4zoc3#
用于在Swift 4和Swift 5中查找indexPath时调用按钮,
//首先在“cellForRowAt”中的按钮上添加目标
//添加函数
pes8fvy94#
其中一种方法是,假设您有自定义单元格和单元格内的按钮......通常的模式是:(如果您使用的是带有数据源的表格视图),当在内部配置单元格时:
您可以设置:
所以在:
sender.tag将是sectionIndex
希望你明白。
u0njafvf5#
我最近提出了一个解决这个问题的解决方案,它利用了Segues的使用,并在Swift 2.2和XCode8中实现了它。
我有一个自定义单元格,用于具有2个标签和一个UIButton的标题。我的所有数据都在一个数组中,我希望动态访问这些数据。每个节都是节标题的数组,在该类中,节包含其他数组以完成tableView的生成。我的目标是根据自定义单元格中的按钮单击来获取节的索引(标题)并在不同视图中更改数据。
要用我使用的自定义标题单元格填充tableView,请执行以下操作:
使用故事板时,MyHeaderCell中按钮(addMemberButton)的IBOutlet具有.tag字段,我使用该字段保存节索引。
这里的sender是按钮,因此.tag字段为我们提供了特定的节索引,以便访问数组中的正确位置
希望这能帮助到一些人。
snz8szmq6#
''-在tableview中获取节和行索引的方法之一按钮是:-