简体   繁体   中英

Swift: TableView controller and segment value change

I hope you can help me on this question as I'm a bit stuck...

In my app I have a simple TableView controller with a couple of custom prototype cells.

In one of the custom prototype cells I have a segment control with two values (in my case are temperature scales F/C).

Everything displays fine, I can change the values (using the value changed action) but the problem for comes when I want to 'send' that selection to another ViewController (via segue) from the TableView controller (eg going back to the previous displayed controller and this value will be used for displaying the temperature in the right scale within the label).

If I have a variable within the prototype custom cell (userSelection), this is not accessible within the tableview class.

If I move the 'value change' action to the tableview controller I seem to 'lose' the ability to track what changed.

I am missing something that 'glues' these together...

Many thanks!

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var button1: UIButton!
    @IBAction func pressButton1(sender: AnyObject) {
    }
}


class TableViewController: UITableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
        var cell = tableView.dequeueReusableCellWithIdentifier("customcell") as! TableViewCell
        return cell
    }

    class TableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

    @IBOutlet weak var segment1: UISegmentedControl!

    @IBAction func segmentValueChanged(sender: AnyObject) {
        var userSelection = segment1.selectedSegmentIndex
    }
}

If you only need to inform one other instance at a time, you could use the delegation pattern:

Define a delegate protocol:

protocol TableViewCellSegmentDelegate {
   func segmentValueChanged(sender: UISegmentedControl)
}

In your table view cell, define a delegate variable, which you can notify on the value change:

class TableViewCell: UITableViewCell {
    var delegate: TableViewCellSegmentDelegate?

    @IBAction func segmentValueChanged(sender: UISegmentedControl) {
        if let delegate = delegate {
            delegate.segmentValueChanged(sender)
        }
    }
}

In your table view, in tableView(_:cellForRowAtIndexPath:) :

let cell = tableView.dequeueReusableCellWithIdentifier("customcell") as! TableViewCell
cell.delegate = self
return cell

Implement the protocol in the table view:

extension TableViewController: TableViewCellSegmentDelegate {
    func segmentValueChanged(sender: UISegmentedControl) {
        // do something with `sender.state`
    }
}

If you want to inform multiple instances, you could fire an NSNotification when the value changes.

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