【问题标题】:UISwitch - UITableViewUISwitch - UITableView
【发布时间】: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


【解决方案1】:

吉根,

这是你可以做的:)

在您的单元类中声明一个协议,我们将其称为 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 中,为每个单元格设置委托为自己。

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 进行切换的单元格 :) 现在您只需要从 UISwitch 到单元格的 IBAction :)

@IBAction func switchTapped(sender: UISwitch) {
        self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)
    }

就是这样:)

【讨论】:

  • @jigen : 好的哥们:)
  • 协议中myCell是什么意思?
  • @jigen :抱歉 myCell 是您的情况下的单元格类的名称,它将是 TicketsFilterCell :)
  • 好吧,我试过了,但我在弱 var 委托上得到了 nil:CellProtocol!。我解决了设置 'protocol CellProtocol: class {...}'
  • @jigen : 你是否将 cellForRowAtIndexPath 中的单元格委托设置为 cell.delegate = self ???
【解决方案2】:

使用这个

cell.yourSwitch.tag=indexPath.row;

cellForRowAtIndexPath方法中

基于该标签执行您的代码在开关操作中。

【讨论】:

  • 谢谢,我试试。
【解决方案3】:

@IBAction 从开关添加到视图控制器本身。在 IBAction 函数中,将sender(UISwitch) 的位置转换为 tableView 的框架。然后,在 tableView 中找到该点的单元格。

我相信这是向表格视图单元格添加动作的最优雅的方式。

  • tag 属性的最大问题是它的使用总是一开始很简单,但随着时间的推移,逻辑(或缺乏逻辑)演变成一大堆难以理解的混乱。
  • 这比创建协议和实现委托更容易

下面是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)
}

【讨论】: