2015-05-18 3 views
0

Я пытаюсь передать объект в 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 

     } 

     } 


    } 

}

+0

Это странная ошибка, так как там ничего не происходит. Как определяется 'tableView'? –

+0

Спасибо, что посмотрели, @ClaudioRedi ... Я добавил определение tableview в исходное сообщение выше. – hudsonian

+0

Нет, он имеет в виду, что такое 'self.tableView'. Этот код работает в UITableViewController? – matt

ответ

2

Проблема заключается в том, что в этой строке:

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

вы не правильно использовали имя собственности вашей точки зрения контроллера. Его имя myTableView. Поэтому измените его так:

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

Проблема решена!

+0

Спасибо за помощь, @matt. Мой код все еще не компилируется, но я буду продолжать это делать, и я рад узнать о сбое глюков/кешей. – hudsonian

+0

@ matt - большое вам спасибо за то, что нашли время, чтобы сделать это. я действительно ценю это, и это помогло мне выйти из привязки, которую я застрял много часов. основные точки кармы, сэр! – hudsonian

+1

Удивительно, насколько слепой может быть такой вид. Ты этого не видел, я этого не видел! Проблема, конечно же, в том, что сообщение об ошибке «Свифт» заставило нас искать не то место. Он понятия не имел, что такое 'self.tableView', так почему же он так не сказал? Плохо! :) – matt

Смежные вопросы