hudsonian
hudsonian

Reputation: 449

Passing object from one scene to next

I'm trying to pass an object in Swift from one scene to the next, and I'm getting an error with the code below saying:

Cannot invoke indexPathForSelectedRow with no arguments.

Is this a new requirement?

It seems to me the code below should work, but I'm confused as to why it isn't.

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

        }

        }


    }

}

Upvotes: 0

Views: 159

Answers (1)

matt
matt

Reputation: 535989

The problem is that in this line:

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

you have not correctly used the name of your view controller's property. Its name is myTableView. So change it to this:

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

Problem solved!

Upvotes: 2

Related Questions