响应 UIViewController 中的表格单元格操作

Respond to a UITableCell action in the UIViewController

所以我有一个 UITableViewCell:

代码如下:

import Foundation
import UIKit

class Cell_Switch: UITableViewCell{

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var select: UISwitch!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    override var layoutMargins: UIEdgeInsets {
        get { return UIEdgeInsetsZero }
        set(newVal) {}
    }
    @IBAction func onSwitch(sender: AnyObject) {
        //I don't want to respond here!!!
    }
}

这是 UITableView 代码:

import Foundation
import UIKit

class PreferencesInterestedDays: UIViewController,UITableViewDataSource,UITableViewDelegate{
    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var nib = UINib(nibName: "Cell_Switch", bundle: nil)
        table.registerNib(nib, forCellReuseIdentifier: "Cell_Switch")
        table.separatorInset = UIEdgeInsetsZero

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }





    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:Cell_Switch = self.table.dequeueReusableCellWithIdentifier("Cell_Switch") as Cell_Switch

        //what do I do here???
        //not working: cell.select.actionsForTarget(self, forControlEvent: UIControlEvents.ValueChanged)



        return cell
    }
    //this event doesn't fire!
    @IBAction func onSwitch(sender: AnyObject) {
        //doesn't work... ;(
        println("Hello!")
    }



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        table.deselectRowAtIndexPath(indexPath, animated: false)
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44.0
    }
}

这是它的样子:

所选的每个开关都有不同的响应。我需要处理 UITableView class 中的操作(并获取行号),而不是 UITableViewCell class...

怎么办?

XCode6.2,iOS8.2

您应该将您的操作委托给 Cell,并在委托方法中发送它自己。 就像 TableView 通过 dataSource 和 Delegate 使用您的视图控制器一样。

这篇文章对你有用 - https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

简单的解决方案是

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:Cell_Switch = self.table.dequeueReusableCellWithIdentifier("Cell_Switch") as Cell_Switch

        cell.select.addTarget(self, action: Selector("onSwitch:"), forControlEvents: UIControlEvents.ValueChanged)

        return cell
    }

    @IBAction func onSwitch(sender: AnyObject) {
        println("Hello!")
    }

注意,删除 Cell_Switch.

中对 IBAction 的任何引用

干杯。

在您的 cellForRowAtIndexPath 中手动为每个开关添加目标

cell.select.addTarget(self, action:"onSwitch:", forControlEvent: .ValueChanged)

然后

func onSwitch(sender: UISwitch){
    //do stuff
}

其他回复解释了如何添加动作方法。所以我将解释您应该如何实现该操作方法。

func onSwitch(sender: UISwitch) {
    let point = tableView.convertPoint(CGPoint.zeroPoint, fromView: sender)
    if let indexPath = tableView.indexPathForRowAtPoint(point) {
        // Now you have indexPath. You now know which UISwitch sends that action.
    }
}