Swift显示来自prepareForSegue的传递数据

时间:2014-12-21 19:52:32

标签: xcode swift

好的,我第一次在这里寻求帮助,通常我可以搜索并找到答案但不是这次。我有一张桌子,显示图片和名字。如果选择其中一个,它将转到另一个视图并且数据通过。但是,我试图将传递的信息显示在如下表中:Name: (Passed Info), Age: (Passed Info), Gender: (Passed Info)等。我知道数据通过,因为我可以在标签中显示信息,但我无法弄清楚如何将其传递给显示在一张桌子里。 Index问题? String问题?

这是传递信息的代码:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var detailsViewController: DetailsViewController = segue.destinationViewController as DetailsViewController
    var horseIndex = appsTableView!.indexPathForSelectedRow()!.row
    var selectedHorse = self.horses[horseIndex]
    detailsViewController.horse = selectedHorse

这是控制器上的代码,用于获取我希望表格显示的数据

class DetailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!
@IBOutlet weak var titleLabel: UILabel!

var horse: Herd?


required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel?.text = self.horse[indexPath.Row]

    return cell
}

}

我在return self.horse.count上收到错误,指出不是成员,self.horse[indexPath.Row]上的错误NSIndex没有成员命名行。

我觉得我没有正确地展开它或其他东西,但我无法弄清楚或在我的搜索中找到答案。如果您需要更多信息,请告诉我并提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

<强>澄清...

你在segue方法中正确地抓住self.horses[horseIndex],所以你已经完成了获得1匹特定马的工作。无需再做一次。 self.horsesHerd类型相关联吗?混淆了为什么Herd再次出现在DetailView中 - 您似乎并不需要它。

听起来你现在真正想要的是马细节的表格布局......正确吗?就像从你的整个&#34;联系人&#34;列表到单个联系人的表格视图?

不再涉及多匹马的数组,因此您无需使用Herd?herd.count。使用静态标签或静态tableView显示1 Horse的信息。

您的数据结构是什么&amp;详情是什么?

大概你要创造的东西(如果你还没有)是Horse类型:

class Horse {
       //you could use optional properties here, or require them for a fully initialized Horse.
       let name:String?
       let gender:String?
       let breed:String?
       var age:Int?
       var restingHeartRate:Int?

       init(name:String?, gender:String?, breed:String?, age:Int?, restingHeartRate:Int?) { 
         //set arguments passed in to respective properties of self... 
      } 

       func winTripleCrown() {
         println("\(name!) Wins! Suck on that, Sea Biscuit!")
       }
    } 

确保您的horses数组声明为仅采用Horse个实例,并正确填充:

var horses = [Horse]()
let horse1 = Horse(name:"Bob" gender:"male" breed: "Yessir, I am a real horse" age:42 restingHeartRate: 30)
horses.append(horse1)
println(horses.count) //prints "1"
horses[0].winTripleCrown() //prints: "Bob Wins! Suck on that, Sea Biscuit!"

在DetailViewController中使用Horse Type而不是Herd

var horse: Horse? //nil until set in prepareForSegue  

然后在segue方法中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    ...
    detailsViewController.horse = selectedHorse

现在,假设您只将正确创建的Horse实例放入初始数组中,并且在selectedIndex中有一个实例,那么您可以保证DetailViewController中的horse变量是Horse 1}}实例,并且它是在第一个&#34;整体&#34;中选择的实例。的tableView。

创建详细视图

轻松: 此时的简单解决方案是创建带有标签,图像等的detailView布局,并将它们连接到DetailViewController中的@properties。然后将Horse属性映射到@IBOutlets。完成。不需要再使用tableViews了 - 只是伪造它或使用scrollView使它看起来像一个表。

TABLEVIEW ROUTE 但是如果你想使用UITableView,那么你需要像你一样使用的委托方法......区别在于你需要查看单个Horse实例的属性数量 - 与Herd或总数组中的整体马匹列表无关。

如果您确定#和Horse属性的顺序始终保持一致,您可以对行数进行硬编码:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 5 //1 row per property in a `Horse` instance as defined above
}

但是,如果您想要考虑更多动态结果(例如一系列获胜/亏损或可变长度的照片库),您需要有足够的单元格来映射所有Horse属性。如何使用不是数组或字典的自定义类型?

在Swift中,您可以通过创建"Mirror" type并将其用于内省来确定Type有多少属性,以便将信息反映给您,就像上面缺少的count属性一样。

let horseMirror = reflect(self.horse)

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.horseMirror.count //should also return "5" but no longer hard-coded
}

然后在cellForRowAtIndexPath:中,您可以打开indexPath,将各种Horse属性分配给tableView单元格中的标签(或图像等)。如果要显示不同的信息,请创建具有唯一标识符的自定义tableView单元格类型,或者使用内置选项粘贴:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

   var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

   //haven't played w/this to see if the optionals are correct. Consider it psuedocode...
    switch indexPath.row {
        case 1: cell.textLabel.text = self.horse.name?
        case 2: cell.textLabel.text = self.horse.gender?
        case 3: cell.textLabel.text = self.horse.breed?
        case 4: cell.textLabel.text = String(self.horse.age?)
        case 5: cell.textLabel.text = String(self.horse.restingHeartRate?)
        default: cell.textLabel.text = ""
    }

    return cell
}