在表视图中为不同的视图控制器准备segue函数

时间:2015-07-21 09:18:12

标签: swift tableview segue navigationcontroller

当我点击表格视图的行时,应该在不同的视图控制器中编辑详细信息。我的准备segue功能有问题。以及在新的不同控制器中编写代码的位置,以便在此页面中查看。

import UIKit

class VehiclesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{


    var tableView:UITableView?
    var items = NSMutableArray()

    override func viewWillAppear(animated: Bool) {
        let frame:CGRect = CGRect(x: 0, y: 100, width: self.view.frame.width, height: self.view.frame.height)
        self.tableView = UITableView(frame: frame)
        self.tableView?.dataSource = self
        self.tableView?.delegate = self
        self.view.addSubview(self.tableView!)
        addDummyData()
    }

    func addDummyData() {
        self.items.removeAllObjects()
        RestApiManager.sharedInstance.getVehicleList { json in
            let results = json
            println(results.description)
            for (index: String, subJson: JSON) in results {
                let vehicle: AnyObject = subJson.object;
                self.items.addObject(vehicle);
                dispatch_async(dispatch_get_main_queue(), {
                    tableView?.reloadData()
                });
            }
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var secondScene = segue.destinationViewController as! VehicleEditViewController

        if let indexPath = self.tableView?.indexPathsForSelectedRows(){
            let selectedVehicle: AnyObject = self.items[indexPath.row]
            secondScene.detailedDescriptionLabel = selectedVehicle as! UILabel
        }

    }



     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
        }

        println("something about the table");

        let vehicle:JSON = JSON(self.items[indexPath.row])
        cell!.textLabel?.text = vehicle["vrn"].string


        let status = vehicle["liveView"]["currentStatus"].string
        cell!.backgroundColor = getColorForStatus(status!)

        var stateString = ""
        var speedIn = " mph"
        switch(status!) {
            case "moving":
                stateString =  vehicle["liveView"]["gpsPosition"]["speed"].stringValue + speedIn
                break;
            case "idle":
                stateString = vehicle["liveView"]["lastState"].string!
                break;
            case "stop":
                 stateString = "Stationary"
                break;
            default:
                stateString = "State Unknown"
        }
        cell?.detailTextLabel?.text = stateString


        return cell!
    }



    func getColorForStatus(status : String) -> UIColor {
        switch(status) {
            case "moving":
                return UIColor.greenColor();
            case "idle":
                return UIColor.yellowColor();
            case "stop":
                return UIColor.redColor();
            default:
                return UIColor.whiteColor()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你没有performSegueWithIdentifier。 试试这个:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
        performSegueWithIdentifier("yourSegueNameToEditVehicles", sender : indexPath) }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var secondScene = segue.destinationViewController as! VehicleEditViewController

    if let indexPath = sender as! NSIndexPath{
        let selectedVehicle: AnyObject = self.items[indexPath.row]
        secondScene.detailedDescriptionLabel = selectedVehicle as! UILabel
    }

}

确保故事板中两个视图之间有一个好名字的segue。

答案 1 :(得分:0)

但是当我在控制台中打印secondScene.detailedDescriptionLabel时显示为nil。我可以在selectedVehicle中接收我的json对象。但我无法将JSON对象转换为我可以在第二个控制器中使用的任何变量