Seb*_*Seb 3 uitableview ios swift
我有一个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 = ???????
}
Run Code Online (Sandbox Code Playgroud)
您可以通过计算发件人的位置并在该位置找到该行来轻松找到该行 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
}
Run Code Online (Sandbox Code Playgroud)
这就是Apple在Accessory示例中的做法. https://developer.apple.com/library/ios/samplecode/Accessory/Listings/AccessoryViewController_m.html#//apple_ref/doc/uid/DTS40008066-AccessoryViewController_m-DontLinkElementID_4
在 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7335 次 |
| 最近记录: |