【问题标题】:Select UITableView's row when tapping on its UISwitch in Swift在 Swift 中点击 UISwitch 时选择 UITableView 的行
【发布时间】:2015-06-13 12:48:51
【问题描述】:

这个问题已经在 Objective-C 中处理过here。但我在 Swift 工作并且有一个类似的问题。

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

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

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

视图控制器:

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
    }

基于 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")
    }

产生这个(请忽略格式):

【问题讨论】:

  • 我在这里stackoverflow.com/questions/29354969/…回答了一个类似的问题。希望对您有所帮助。
  • @VictorSigler 我会看看你的帖子,谢谢 Victor。看起来我在发布前搜索时错过了这个。

标签: ios iphone uitableview swift uiswitch


【解决方案1】:

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

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)
        ^^^^
    }
}

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

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)
        ...
    }
}

当点击开关时,事件被传播到视图控制器,新的状态和单元格本身作为参数传递。从单元格中你可以获取索引路径,然后做你需要做的任何事情,比如选择行等。

【讨论】:

  • 我有一个疑问,var cellDelegate: SettingCellDelegate? 它不会被声明为weak 以避免内存循环?我想知道你对这件事的看法,请
  • @VictorSigler:你是绝对正确的 - 它必须是 weak,但为了使其工作,协议必须声明为类协议。
  • @Antonio 感谢您的回答,您澄清了我!不错
  • 优秀的答案,@Antonio。也许在handledSwitchChange函数中你可以有self.cellDelegate?.didChangeSwitchState(sender: self, isOn:sender.on) 因此不需要settingsSwitch插座。
猜你喜欢
  • 2012-10-18
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多