响应UIViewController中的UITableCell操作

时间:2015-03-13 10:28:59

标签: ios uitableview swift xcode6 ibaction

所以我有一个UITableViewCell:

enter image description here

以下是代码:

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

这是它的样子:

enter image description here

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

怎么办?

XCode 6.2,iOS 8.2

4 个答案:

答案 0 :(得分:1)

您应该从Cell委托您的行动并以委托方式发送自己。 就像TableView一样,它通过dataSource和Delegate与你的视图控制器一起完成。

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

答案 1 :(得分:1)

简单的解决方案是

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 的任何引用。

干杯。

答案 2 :(得分:1)

在cellForRowAtIndexPath

中手动为每个开关添加目标
cell.select.addTarget(self, action:"onSwitch:", forControlEvent: .ValueChanged)

然后

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

答案 3 :(得分:1)

其他响应说明了如何添加动作方法。所以我将解释你应该如何实现该动作方法。

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