简体   繁体   中英

Add a Selector from a UITableViewCell class to the UITableViewController class

I'm trying do design my own UITableViewCell to include a Stepper in the AccessoryView. Therefore, I would like to add selector so that the ViewController would handle the behaviour of the value being changed. (The UITableViewCell should only be responsible to display the result in its label)

Currently, I'm able to modify the label's text by using addTarget to the UITableViewCell class, but when I add a target to the UITableViewController class, I get the following error:

2018-10-08 20:04:39.157912-0400 MyProject[1003:45273] -[MyProject expansionSelectionValueDidChange:]: unrecognized selector sent to instance 0x7fa88700fc00
2018-10-08 20:04:39.167219-0400 MyProject[1003:45273] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyProject.PlayerSelectionCell expansionSelectionValueDidChange:]: unrecognized selector sent to instance 0x7fa88700fc00'

I'm using X-Code 10 and Swift 4 to develop an iOS 12 application.

Here's the code to the UITableViewCell :

class PlayerSelectionCell: UITableViewCell {

    @objc func stepperValueDidChange(_ sender: UIStepper) {
        textLabel?.text = "\(newValue) Players"
    }

    override func awakeFromNib() {
        let stepper = UIStepper()
        stepper.addTarget(self, action: #selector(stepperValueDidChange(_:)), for: .valueChanged)
        stepper.addTarget(self, action: #selector(SetupViewController.expansionSelectionValueDidChange(_:)), for: .valueChanged)
        self.accessoryView = stepper
    }
}

Here's the code to my UITableViewController

class SetupViewController: UITableViewController {

    @IBOutlet var setupTableView: UITableView!

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            return "Expansions"
        case 1:
            return "Players"
        default:
            return nil
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return Expansions.allCases.count
        case 1:
            return 3
        default:
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Expansion Selection", for: indexPath) as? ExpansionCell ?? ExpansionCell()
            cell.textLabel?.text = Expansions.allCases[indexPath.row].rawValue
            return cell
        } else if indexPath.section == 1 && indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Player Selection", for: indexPath) as? PlayerSelectionCell ?? PlayerSelectionCell()
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Player Detail", for: indexPath) as? PlayerCell ?? PlayerCell()
            cell.textLabel?.text = "Player \(indexPath.row)"
            return cell
        }
    }

    @objc func playerCountValueDidChange(_ sender: UIStepper) {
        print("Value : \(sender.value)")
    }

    @objc func expansionSelectionValueDidChange(_ sender: UISwitch) {
        print("Value : \(sender.isOn)")
    }

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

In alternative to the accepted solution:

Depending on case you can try use delegate instead action/target

//Declare a delegate for the cell
protocol PlayerSelectionCellDelegate : class {
    func playerSelectionCell(_ cell:PlayerSelectionCell, didChangeValue:Double)
    func playerSelectionCell(_ cell:PlayerSelectionCell, expansionSelectionValueDidChange:Bool)
}

class PlayerSelectionCell: UITableViewCell {

    weak var delegate : PlayerSelectionCellDelegate?        //Set a weak var to allocate the delegate

    @objc func stepperValueDidChange(_ sender: UIStepper) {
        delegate?.playerSelectionCell(self, didChangeValue: sender.value)
    }

    @objc func expansionSelectionValueDidChange(_ sender: UISwitch) {
        delegate?.playerSelectionCell(self, expansionSelectionValueDidChange: sender.isOn)
    }

    override func awakeFromNib() {
        let stepper = UIStepper()
        stepper.addTarget(self, action: #selector(stepperValueDidChange), for: .valueChanged)

        //This line should be reference to the UISwitch Action
        selectionSwitch.addTarget(self, action: #selector(expansionSelectionValueDidChange), for: .valueChanged)
        self.accessoryView = stepper
    }
}

In the ViewController you change the code to get in conformity to the delegate and set the ViewController as delegated to cell

class SetupViewController: UITableViewController {

    @IBOutlet var setupTableView: UITableView!

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            return "Expansions"
        case 1:
            return "Players"
        default:
            return nil
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return Expansions.allCases.count
        case 1:
            return 3
        default:
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Expansion Selection", for: indexPath) as? ExpansionCell ?? ExpansionCell()
            cell.textLabel?.text = Expansions.allCases[indexPath.row].rawValue
            return cell
        } else if indexPath.section == 1 && indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Player Selection", for: indexPath) as? PlayerSelectionCell ?? PlayerSelectionCell()
            cell.delegate = self            //Set the ViewController to be delegated by cell
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Player Detail", for: indexPath) as? PlayerCell ?? PlayerCell()
            cell.textLabel?.text = "Player \(indexPath.row)"
            return cell
        }
    }

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

    //Remove the actions and implements the delegate methods
    func playerSelectionCell(_ cell: PlayerSelectionCell, didChangeValue players: Double) {
        print("Value Players: \(players)")
    }

    func playerSelectionCell(_ cell: PlayerSelectionCell, expansionSelectionValueDidChange selected: Bool) {
        print("Value Selected: \(selected)")
    }

}

Actually, the problem is that the addTarget(_ target, ...) method invokes the selector on target . Even if you specify it like you did with full path.

What you can try is to set target to nil and it should work then if the documentation is right:

The target object—that is, the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.

And if that doesn't help, specifying the target exactly should solve it. You will just have to pass a reference to the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM