运行时prepareForSeague错误

时间:2015-05-14 11:24:32

标签: swift

我正在创建一个应用程序,允许用户点击UITableView中的项目,然后进入新屏幕,其中包含他们点击的内容的信息。我在尝试运行时收到一个错误。

import UIKit

class CarTableViewController: UITableViewController {

var cars = [Cars]()

override func viewDidLoad() {
    super.viewDidLoad()

    var newCar = Cars(name: "Audi", image: "iphone6", details: "iPhone 6, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Bmw", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Bugatti", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Mercedes", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Lamboghini", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Ferrari", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

    newCar = Cars(name: "Bmw", image: "iphone6plus", details: "iPhone 6 Plus, Apple, 2015")
    cars.append(newCar)

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

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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 Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return cars.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

        var currentCars = cars[indexPath.row]
        cell.textLabel?.text = currentCars.carName

    return cell
}


/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
}
*/


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var nextScene = segue.destinationViewController as! DisplayViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
        let selectedCar = cars [indexPath.row]
        nextScene.selectedCar = selectedCar
}
}

我在收到预期声明的底线收到错误。我已经尝试了很多方法来修复它,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

你这样试试 -

您可以使用委托方法didSelectRowAtIndexPath

而不是麻烦地附加segue和all
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        var nextScene = DisplayViewController()
        let selectedCar = cars [indexPath.row]
        nextScene.selectedCar = selectedCar

       //then push the view controller on top of your current view controller
        self.navigationController.pushViewController(nextScene, animated: true)
  }

当然,您需要符合UITableviewDelegate。