SWIFT - TableViewController根据行选择查看另一个TableViewController

时间:2015-11-24 15:43:01

标签: ios swift uitableview tableview

我有一个TableViewController(让我们称之为TVC1)有三行 - OD,Weight,ID

当选择其中一个时,它将转到另一个TableViewController(TVC2)以向用户显示可供选择的选项。无论从TVC1中选择哪一行,它总是会转到TVC2。所以我将使用枚举,因此我可以根据用户的选择更改用户可用的选项。但是,当我运行应用程序时,我遇到了一个崩溃,说“在展开可选值时意外地发现了nil”并且它指向了我的TVC1代码中的view2.selectedData = selectedOption

我的TVC2代码是:

import UIKit

enum Data {
case OuterDiameter
case Weight
case InnerDiameter
}

class CasingSelectionTableViewController: UITableViewController {

var selectedData = Data.OuterDiameter

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"]

let casingWeight114 = ["14.14", "15.63", "17.26", "20.09", "22.47", "24.70", "26.34", "27.97"]
let casingWeight127 = ["17.11", "19.35", "22.32", "26.79", "31.85", "34.53", "35.86"]
let casingWeight139 = ["20.83", "23.07", "25.30", "29.76", "34.23", "39.28", "44.20", "48.51", "52.53", "56.55", "60.27", "64.14"]
let casingWeight168 = ["19.34", "25.30", "29.76", "32.74", "35.71", "38.69", "41.66", "43.15", "47.62", "50.59"]
let casingWeight177 = ["25.30", "29.76", "34.23", "38.69", "43.16", "47.62", "52.09", "56.55"]
let casingWeight193 = ["35.72", "39.29", "44.20", "50.15", "58.04"]
let casingWeight219 = ["35.72", "41.67", "47.62", "53.57", "59.53"]
let casingWeight244 = ["48.07", "53.57", "59.53", "64.73", "69.94", "79.62"]
let casingWeight247 = ["88.09"]
let casingWeight273 = ["48.74", "60.27", "67.71", "75.90", "82.59"]
let casingWeight298 = ["56.54", "62.50", "69.94", "80.35", "89.28", "96.72", "105.65"]
let casingWeight339 = ["71.43", "81.10", "90.78", "101.19", "107.15"]
let casingWeight406 = ["96.73", "111.61", "125.01", "162.21"]
let casingWeight473 = ["108.76", "116.06", "130.00", "143.59"]
let casingWeight508 = ["139.89", "158.49", "197.93"]

let id114 = ["103.886", "102.921", "101.600", "99.568", "97.180", "95.300", "93.900", "92.400"]
let id127 = ["115.824", "114.148", "111.963", "108.610", "104.800", "104.800", "102.718", "101.600"]
let id139 = ["127.305", "125.730", "124.257", "121.361", "118.618", "114.300", "111.150", "107.950", "104.800", "101.600", "98.450", "95.520"]
let id168 = ["158.800", "155.800", "153.600", "152.100", "150.300", "148.700", "147.000", "146.300", "144.100", "142.100"]
let id177 = ["166.065", "163.982", "161.696", "159.410", "157.074", "154.788", "152.502", "150.368"]
let id193 = ["178.400", "177.000", "174.700", "171.900", "168.300"]
let id219 = ["205.664", "203.632", "201.193", "198.700", "197.400"]
let id244 = ["228.630", "226.590", "224.410", "222.380", "220.500", "216.790"]
let id247 = ["217.400"]
let id273 = ["258.877", "255.270", "252.730", "250.190", "247.904"]
let id298 = ["283.200", "281.500", "279.400", "276.300", "273.600", "271.300", "268.800"]
let id339 = ["322.961", "320.421", "317.881", "315.341", "313.614"]
let id406 = ["387.350", "384.150", "381.254", "373.075"]
let id473 = ["454.000", "453.500", "450.900", "448.400"]
let id508 = ["485.750", "482.600", "475.742"]

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(animated: Bool) {

    switch selectedData {

    case .OuterDiameter:
        print(casingOD)

    case .Weight:
        print(casingWeight114)

    case .InnerDiameter:
        print(id114)

    }
}

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

我在TVC1的代码是:

import UIKit

class CapacitiesTableViewController: UITableViewController {

var selectedOption: Data!
let capacityParameters = ["O.D", "Weight", "I.D"]

@IBAction func cancelSelection(segue: UIStoryboardSegue) {

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 125.0
}

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 0 {
        return capacityParameters.count
    } else {
        return 1
    }

}


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

    // init return type of cell
    var returnCell: UITableViewCell!

    if indexPath.section == 0 { // you can also check for section with section.index
        returnCell = tableView.dequeueReusableCellWithIdentifier("capacitiesCell", forIndexPath: indexPath)

        let capacityID = capacityParameters[indexPath.row]
        returnCell.textLabel?.text = capacityID

    return returnCell

    } else {

        returnCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)

        return returnCell

    }
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {
        selectedOption = Data.OuterDiameter
    } else if indexPath.row == 1 {
        selectedOption = Data.Weight
    } else {
        selectedOption = Data.InnerDiameter
    }
    self.performSegueWithIdentifier("capacitiesSelectedSegue", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let destinationView = segue.destinationViewController as? UINavigationController {
      let view2 = destinationView.topViewController as! CasingSelectionTableViewController
        view2.selectedData = selectedOption
    }

}

0 个答案:

没有答案