简体   繁体   English

Swift:TableView控制器和段值更改

[英]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. 在我的应用程序中,我有一个简单的TableView控制器,其中包含几个自定义原型单元。

In one of the custom prototype cells I have a segment control with two values (in my case are temperature scales F/C). 在一个自定义原型单元格中,我有一个带有两个值的分段控件(在我的情况下是温度标度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). 一切都显示正常,我可以更改值(使用值更改操作),但是当我想将选择从TableView控制器(通过segue)“发送”到另一个ViewController时(例如,返回到先前显示的控制器),问题就来了并且此值将用于在标签内以正确的比例显示温度)。

If I have a variable within the prototype custom cell (userSelection), this is not accessible within the tableview class. 如果我在原型自定义单元格(userSelection)中有一个变量,则无法在tableview类中访问此变量。

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:) : 在表格视图中,在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. 如果要通知多个实例,则可以在值更改时触发NSNotification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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