SWIFT - UITableViewCell 根据选择更新

SWIFT - UITableViewCell updating based on selection

我有一个 TableViewController(我们称之为 TVC1),其中一行显示 "OD"(代表外径)。

选择此行后,将显示包含各种 OD(我的代码中的 casingOD)的新 TableViewController(我们称之为 TVC2)中的一堆行。我想要发生的是,当用户选择 OD 时,它将使用与用户选择相对应的字符串返回到主 TableViewController。我的代码目前失败了……有人能帮我指出正确的方向吗?如果您需要 TVC1 代码,我会很乐意 post 它,我只是想为你们节省任何不必要的代码阅读:)

我的TVC2代码如下:

import UIKit

class CasingSelectionTableViewController: UITableViewController {

var selectedData: Data?

let casingOD = ["114.3", "127.0", "139.7", "168.3" , "177.8", "193.7", "219.1", "244.5", "247.6", "273.1", "298.4", "298.4", "339.7", "406.4", "473.0", "508"]

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

override func viewWillAppear(animated: Bool) {
    switch selectedData! {

    case .OuterDiameter:
        print(casingOD)

    case .Weight:
        print(casingWeight114) // I deleted the casingWeight114 line of code as its not required for this question

    case .InnerDiameter:
        print(id114) // I deleted the id114 line as its not required for this question
    }
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return casingOD.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var casingSpec: UITableViewCell!

    if selectedData == Data.OuterDiameter {
        casingSpec = tableView.dequeueReusableCellWithIdentifier("selectedCasingSpec", forIndexPath: indexPath)
        let casingODSpec = casingOD[indexPath.row]
        casingSpec.textLabel?.text = casingODSpec
        return casingSpec
    } else {
        return casingSpec
    }

}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selection: UITableViewCell!
    selection.textLabel?.text = indexPath.row as! String

}

What I want to happen is when the user selects the OD it will segue back to the main TableViewController with the string that corresponds to the user selection.

首先,您需要实现一种方法 TVC2 通知 TVC1 已选择一个值。

做这种事情的一种常见方法是使用委托。您可以像这样定义委托协议:

protocol TVC2Delegate {
    func tvc2(tvc2: TVC2, didSelectOuterDiameter outerDiameter: String)
}

然后在TVC2中添加一个var delegate: TVC2Delegate?属性.

然后您将通过在 TVC1 中实施该方法使 TVC1 符合 TVC2Delegate

当从 TVC1 展示 TVC2 时,记得将其设置为 TVC2 的代表。

// In TVC1
tvc2.delegate = self

要连接 TVC1TVC2,您可以在 tableView(tableView:,didSelectRowAtIndexPath:) 方法中添加一些逻辑,使用选定的值

调用委托
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let stringValue = indexPath.row as! String

    // Do anything you need to do related to TVC2 here.
    // Then finally

    delegate?.tvc2(self, didSelectOuterDiameter: stringValue)
}

最后,在 TVC1 的委托方法实现中,如果需要,您可以注意关闭 TVC2

更新:

这就是这些位的最终实现方式:

// In TVC1
class TVC1: UITableViewController, TVC2Delegate {

    // ...

    // Implement the method(s) of TVC2Delegate
    func tvc2(tvc2: TVC2, didSelectOuterDiameter outerDiameter: String) {
        // Do whatever you need to do with the outerDiameter parameter
    }

}

// In TVC2
protocol TVC2Delegate {
    func tvc2(tvc2: TVC2, didSelectOuterDiameter outerDiameter: String)
}

class CasingSelectionTableViewController: UITableViewController {

    var delegate: TVC2Delegate?

    // ...

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let stringValue = casingOD[indexPath.row]

        // Do anything you need to do related to TVC2 here.
        // Then finally

        delegate?.tvc2(self, didSelectOuterDiameter: stringValue)
    }
}

按照@Mokagio 的回答建议使用委托方法。如果您在获取字符串时遇到问题,这里是答案

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let cell = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell
let stringValue = cell.textLabel.text  //You can get this from your datasource as well)

//call the delegate

}