gpt4 book ai didi

swift - 将 TableView 中的 segue 函数准备到不同的 View Controller

转载 作者:行者123 更新时间:2023-11-30 14:13:03 25 4
gpt4 key购买 nike

当我单击 TableView 的行时,应该在不同的 View Controller 中编辑详细信息。我的准备转场功能有问题。以及在新的不同 Controller 中编写代码的位置,以便在此页面中查看。

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()
}
}
}

最佳答案

您没有 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
}

}

确保 Storyboard中两个 View 之间有一个具有良好名称的 Segue。

关于swift - 将 TableView 中的 segue 函数准备到不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com