将对象从一个场景传递到下一个场景

时间:2015-05-18 15:34:20

标签: ios swift

我正在尝试将Swift中的一个对象从一个场景传递到另一个场景,我在下面的代码中遇到错误:

  

无法调用不带参数的indexPathForSelectedRow。

这是新要求吗?

在我看来,下面的代码应该可行,但我很困惑为什么不是。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!

var propertyArray: [Property] = [Property]()


override func viewDidLoad() {
    super.viewDidLoad()

    self.setUpProperties()

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func setUpProperties() {

    var property1 = Property(cardImage: "image1.png", name: "Name1", location: "Hollywood")
    var property2 = Property(cardImage: "image2.png", name: "Name2", location: "Astoria")
    var property3 = Property(cardImage: "image3.png", name: "Name3", location: "Ft. Greene")

    propertyArray.append(property1)
    propertyArray.append(property2)
    propertyArray.append(property3)

}

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

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

    let property = propertyArray[indexPath.row]

    println(property.name + " " + property.location)

    cell.setProperty(property.cardImage, name: property.name, location: property.location)

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {

        var detailPage = segue.destinationViewController as! DetailViewController

        if let indexPath = self.tableView.indexPathForSelectedRow() {

        let selectedProperty = propertyArray[indexPath.row]
        detailPage.currentProperty = selectedProperty

        }

        }


    }

}

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

if let indexPath = self.tableView.indexPathForSelectedRow() {

您没有正确使用视图控制器属性的名称。它的名字是myTableView。所以改成它:

if let indexPath = self.myTableView.indexPathForSelectedRow() {

问题解决了!