0

when I click on the row of the table view should edit with details in the different view controller. Something wrong with my prepare segue function. And also where to write code in the new different controller to view in this page.

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()
        }
    }
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Raj
  • 61
  • 3
  • 7

2 Answers2

0

You dont have performSegueWithIdentifier. Try this:

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
    }

}

Be sure you have a segue with the good name between your two view in your storyboard.

Persilos
  • 122
  • 1
  • 16
  • Thank you for your answer. but when i print in console secondScene.detailedDescriptionLabel which is showing nil. i can receive my json object in the selectedVehicle. but i am not able to convert the JSON object to any variablw where i can use in the second controller – Raj Jul 21 '15 at 12:05
0

but when i print in console secondScene.detailedDescriptionLabel which is showing nil. i can receive my json object in the selectedVehicle. but i am not able to convert the JSON object to any variablw where i can use in the second controller

Raj
  • 61
  • 3
  • 7