2014-12-21 88 views
2

好的,我第一次在這裏尋求幫助時,通常我可以搜索並找到我的答案,但現在還沒有。我有一個顯示圖片和名稱的表格。如果其中一個被選中,它將轉到另一個視圖並且數據通過。但是,我試圖讓傳遞的信息顯示在如下表中:Name: (Passed Info), Age: (Passed Info), Gender: (Passed Info)等我知道數據通過,因爲我可以在標籤中顯示信息,但我無法弄清楚如何讓它顯示在表中。 Index問題? String問題?Swift顯示從prepareForSegue傳遞的數據

這是傳遞信息代碼:

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沒有名爲row的成員。

我覺得我沒有正確解開它或什麼東西,但我無法弄清楚或在我的搜索中找到答案。如果您需要更多信息,請提前通知我並提前感謝您的幫助。

+0

變化'self.horse.count'到'self.horse?.count? 0'和'self.horse [indexPath.Row]'到'self.horse?[indexPath.row]'。 – bluedome

+0

Herd是Array的一個子類嗎?還是它有一個你已經實現的'count'屬性?錯誤告訴你,XCode在'Herd'實例'self.horse'上找不到那些屬性。這不是關於可選項(無或不可),而是關於使用自定義類型「Herd」,它沒有您要求的值。 有點難理解爲什麼detail ViewController具有第二個表格視圖,並且如果您只傳遞前一個視圖中的單個馬匹,則需要「Herd」。 – mc01

+0

@bluedome謝謝你的回覆,我提出了你的建議修改:關於count我得到Herd沒有一個名字爲count的成員我得到Herd沒有一個名爲subscript的成員。 –

回答

0

澄清...

您正確搶self.horses[horseIndex]在SEGUE的方法,所以你已經完成的工作,以獲得1名特異性馬。不需要再做一次。 self.horses是否與Herd類型綁定?混淆爲什麼Herd再次出現在DetailView中 - 你似乎並不需要它。

這聽起來像你在這一點上真正想要的是一個表格佈局的細節馬......對不對?就像從整個「聯繫人」列表轉到單個聯繫人的表格視圖一樣?

不再涉及多匹馬的陣列,所以您不需要使用Herd?herd.count。使用靜態標籤或靜態tableView來顯示1馬的信息。

什麼是你的數據結構&什麼是詳細視圖?

大概你要創建的(如果你還沒有的話)是一個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!" 

使用Horse類型,而不是Herd在DetailViewController:

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

然後在SEGUE方法:

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

現在,假設你只能把正確創建Horse實例到初始數組,selectedIndex處有一個,你保證,在horse變量DetailViewController是一個Horse實例,並且它是第一個「整體」tableView中選擇的實例。

創建詳細視圖

EASY: 在這一點最簡單的解決方案是創建一個的DetailView佈局標籤,圖像等和鉤起來在DetailViewController @properties。然後將Horse屬性映射到@IBOutlets。完成。不再需要混亂w/tableViews - 只是假的或使用scrollView使它看起來像一張桌子。

泰伯維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 
} 

但是,如果要考慮更多的動態效果(如一系列的勝利/損失,或可變長度的照片庫),你需要有足夠的單元格來映射所有的馬屬性。你怎麼用一個不是數組或字典的自定義類型來做到這一點?

在Swift中,您可以通過創建一個"Mirror" 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細胞類型W /唯一標識符,如果你想顯示不同的信息,還是堅持瓦特/內置選項:

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 
} 
+0

聖牛,你爲此付出了很大的努力,我無法感謝你足夠的....我現在在我的路上!再次,謝謝你! –

相關問題