當我點擊表格視圖的行時,應該使用不同視圖控制器中的細節進行編輯。我的準備階段功能有問題。此外,還可以在新的不同控制器中編寫代碼以在此頁面中查看。在表格視圖中爲不同的視圖控制器編寫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()
}
}
}
感謝您的回答。但是當我在控制檯secondScene.detailedDescriptionLabel顯示nil打印。我可以在selectedVehicle中接收我的json對象。但我無法將JSON對象轉換爲任何可以在第二個控制器中使用的變量 – Raj