在Swift中点击其UISwitch时选择UITableView的行 [英] Select UITableView's row when tapping on its UISwitch in Swift

查看:450
本文介绍了在Swift中点击其UISwitch时选择UITableView的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已在Objective-C中此处了解此问题。但我在Swift工作并有类似的问题。

This question has been approached here in Objective-C. But I am working in Swift and have a similar question.

成功创建后,如何在点击其UISwitch时选择UITableView的行?

我的模型中有一个布尔值,并希望根据开关的开/关状态切换该布尔值。

I have a boolean in my model and would like to toggle that boolean based on the switches on/off state.

我有一些以编程方式创建的单元格包含开关......

I have some programmatically created cells that contain switches...

查看控制器:

var settings : [SettingItem] = [
        SettingItem(settingName: "Setting 1", switchState: true),
        SettingItem(settingName: "Setting 2", switchState: true)
    ]

override public func tableView(_tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomSettingCell") as! SettingCell

        let settingItem = settings[indexPath.row]
        cell.settingsLabel.text = settingItem.settingName
        cell.settingsSwitch.enabled = settingItem.switchState!

        return cell
    }

基于a在SettingItem.swift中的模型:

class SettingItem: NSObject {

    var settingName : String?
    var switchState : Bool?

    init (settingName: String?, switchState : Bool?) {
        super.init()
        self.settingName = settingName
        self.switchState = switchState
    } 
}

我在SettingCell.swift中有一些出口:

class SettingCell: UITableViewCell {

    @IBOutlet weak var settingsLabel: UILabel!

    @IBOutlet weak var settingsSwitch: UISwitch!


    @IBAction func handledSwitchChange(sender: UISwitch) {
        println("switched")
    }

产生此效果(请忽略格式):

推荐答案

当我希望事件从单元格传播到包含控制器时,我通常会定义一个自定义委托,如下所示:

When I want events to propagate from a cell to the containing controller, I usually define a custom delegate, like this:

protocol SettingCellDelegate : class {
    func didChangeSwitchState(# sender: SettingCell, isOn: Bool)
}

在单元格中使用它:

class SettingCell: UITableViewCell {
    @IBOutlet weak var settingsLabel: UILabel!
    @IBOutlet weak var settingsSwitch: UISwitch!

    weak var cellDelegate: SettingCellDelegate?

    @IBAction func handledSwitchChange(sender: UISwitch) {
        self.cellDelegate?.didChangeSwitchState(sender: self, isOn:settingsSwitch.on)
        ^^^^
    }
}

在视图控制器中实现协议并在单元格中设置委托:

implement the protocol in the view controller and set the delegate in the cell:

class ViewController : UITableViewController, SettingCellDelegate {
                                              ^^^^
    override func tableView(_tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomSettingCell") as! SettingCell

        let settingItem = settings[indexPath.row]
        cell.settingsLabel.text = settingItem.settingName
        cell.settingsSwitch.enabled = settingItem.switchState!

        cell.cellDelegate = self
        ^^^^

        return cell
    }

#pragma mark - SettingCellDelegate

    func didChangeSwitchState(#sender: SettingCell, isOn: Bool) {
        let indexPath = self.tableView.indexPathForCell(sender)
        ...
    }
}

当点击开关时,事件将传播到视图控制器,具有新状态并且单元格本身作为参数传递。从单元格中,您可以获取索引路径,然后执行您需要的任何操作,例如选择行等。

When the switch is tapped, the event is propagated to the view controller, with the new status and the cell itself passed as arguments. From the cell you can obtain the index path, and then do whatever you need to, such as selecting the row etc.

这篇关于在Swift中点击其UISwitch时选择UITableView的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆