【发布时间】:2026-01-10 01:35:01
【问题描述】:
我有这个tableView:
但我无法识别用户何时选择行中的 UISwitch。 我知道我需要 UITableViewCell 类中的@IBAction,但我没有找到适合我的指南。 当用户点击 UISwitch 时,我想打印该行的 indexPath。
这是我的单元类:
class TicketsFilterCell: UITableViewCell {
@IBOutlet weak var textFilter: UILabel!
@IBOutlet weak var switchFilter: UISwitch!
class var reuseIdentifier: String? {
get {
return "TicketsFilterCell"
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该怎么办?
提前致谢。
编辑
我的 cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: TicketsFilterCell? = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as? TicketsFilterCell
cell?.delegate = self
if(indexPath.section == 0) {
let text = status[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 1) {
let text = queues [indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 2) {
let text = types[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 3) {
let text = severities[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
}
return cell!
}
【问题讨论】:
-
在
IBAction中获取发送者(开关)的superview,这是表格视图单元格。然后获取索引路径。 -
@vadian 谢谢!我会试试这个。
标签: ios swift uitableview uiswitch