我有一个tableView:

但我无法识别用户何时选择行中的UISwitch。我知道我需要UITableViewCell类中的@IBAction,但我没有找到适合我的指南。当用户单击indexPath时,我想打印行的UISwitch。
这是我的牢房课:
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!
}发布于 2016-06-09 10:52:16
吉根
以下是您可以做的事情:)
在单元格类中声明一个协议,让我们将其称为CellProtocol。
protocol CellProtocol : class {
func switchButtonTapped(WithStatus status : Bool, ForCell myCell : TicketsFilterCell)
}在TicketsFilterCell类中声明一个要保存委托的变量,
weak var delegate : CellProtocol!当用户点击开关时,触发委托为
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self)
}在您的TableViewController中确认协议使用
class ViewController: UITableViewController,CellProtocol {现在,在ViewController的cellForRoAtIndexPath中,每个单元格将委托设置为self。
cell.delegate = self最后,按照以下方式实现委托方法
func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) {
let indexPath = self.tableView .indexPathForCell(cell)
print("cell at indexpath \(indexPath) tapped with switch status \(status)")
}编辑:
对于已经拖动IBOutlet For switch :)的每个单元,现在只需要从UISwitch到单元格:)
@IBAction func switchTapped(sender: UISwitch) {
self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)
}(就是这样:)
发布于 2016-06-09 10:53:09
使用这个
cell.yourSwitch.tag=indexPath.row;cellForRowAtIndexPath法
基于该标记,在打开/关闭操作中执行您的代码。
发布于 2016-06-09 11:44:08
将开关中的@IBAction添加到视图控制器本身。在IBAction函数中,将sender(UISwitch)的位置转换为tableView的框架。然后,在tableView中的那个点找到单元格。
我认为这是向表视图单元格添加操作的最优雅的方法。
下面是连接到交换机的IBAction操作valueChanged
@IBAction func switchValueChanged(sender: AnyObject) {
let switchButton = sender as! UISwitch
let buttonPosition = switchButton.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)!
NSLog("switch at index %d changed value to %@", indexPath.row, switchButton.on)
}https://stackoverflow.com/questions/37723672
复制相似问题