2014-10-27 43 views
0

我開發一個簡單的tableViewCustom Cell並在當任一行用戶點擊它定位到下一個DetailViewControllerdetailViewController顯示一個信息,我的代碼的子類型是:「的NSString」不是「的UILabel」

Iteams.swift

class Iteam { 

var name = "name" 
var time = "Time" 
var imageName = "blank" 

init(name : String, time : String, imageName : String){ 

    self.name = name 
    self.time = time 
    self.imageName = imageName 
    } 
} 

DetailViewController.swift

import UIKit 

class DetailViewController: UIViewController { 

@IBOutlet weak var iteamImage: UIImageView! 
@IBOutlet weak var iteamName: UILabel! 
@IBOutlet weak var iteamPrepTime: UILabel! 

var nameStr : String = String() 
var prepTime : Int = Int() 
var iteamImageName : String = String() 


override func viewDidLoad() { 
    super.viewDidLoad() 

    self.iteamName.text = nameStr 
    self.iteamPrepTime.text = String(prepTime) 
    self.iteamImage.image = UIImage(named: iteamImageName) 
    } 
} 

ViewController.swift

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var myTableView: UITableView! 

var arrayOfIteams : [Iteam] = [Iteam]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.setOfIteams() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 
func setOfIteams(){ 

    var iteam1 = Iteam(name: "Egg Benedict", time: "00:10", imageName: "egg_benedict.jpg") 
    var iteam2 = Iteam(name: "Mushroom Risotto", time: "00:20", imageName: "mushroom_risotto.jpg") 
    var iteam3 = Iteam(name: "Full Breakfast", time: "00:30", imageName: "full_breakfast.jpg") 
    var iteam4 = Iteam(name: "Hamburger", time: "00:20", imageName: "hamburger.jpg") 
    var iteam5 = Iteam(name: "Ham and Egg Sandwich", time: "00:10", imageName: "ham_and_egg_sandwich.jpg") 
    var iteam6 = Iteam(name: "Creme Brelee", time: "00:20", imageName: "creme_brelee.jpg") 
    var iteam7 = Iteam(name: "White Chocolate Donut", time: "00:30", imageName: "white_chocolate_donut.jpg") 
    var iteam8 = Iteam(name: "Starbucks Coffee", time: "00:20", imageName: "starbucks_coffee.jpg") 
    var iteam9 = Iteam(name: "Vegetable Curry", time: "00:10", imageName: "vegetable_curry.jpg") 
    var iteam10 = Iteam(name: "Instant Noodle with Egg", time: "00:20", imageName: "instant_noodle_with_egg.jpg") 
    var iteam11 = Iteam(name: "Noodle with BBQ Pork", time: "00:10", imageName: "noodle_with_bbq_pork.jpg") 
    var iteam12 = Iteam(name: "Japanese Noodle with Pork", time: "00:10", imageName: "japanese_noodle_with_pork.jpg") 
    var iteam13 = Iteam(name: "Green Tea", time: "00:20", imageName: "green_tea.jpg") 
    var iteam14 = Iteam(name: "Thai Shrimp Cake", time: "00:10", imageName: "thai_shrimp_cake.jpg") 
    var iteam15 = Iteam(name: "Angry Birds Cake", time: "00:30", imageName: "angry_birds_cake.jpg") 
    var iteam16 = Iteam(name: "Ham and Cheese Panini", time: "00:10", imageName: "ham_and_cheese_panini.jpg") 

    arrayOfIteams.append(iteam1) 
    arrayOfIteams.append(iteam2) 
    arrayOfIteams.append(iteam3) 
    arrayOfIteams.append(iteam4) 
    arrayOfIteams.append(iteam5) 
    arrayOfIteams.append(iteam6) 
    arrayOfIteams.append(iteam7) 
    arrayOfIteams.append(iteam8) 
    arrayOfIteams.append(iteam9) 
    arrayOfIteams.append(iteam10) 
    arrayOfIteams.append(iteam11) 
    arrayOfIteams.append(iteam12) 
    arrayOfIteams.append(iteam13) 
    arrayOfIteams.append(iteam14) 
    arrayOfIteams.append(iteam15) 
    arrayOfIteams.append(iteam16) 
} 

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

    return arrayOfIteams.count 
} 



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

    let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell 

    let food = arrayOfIteams[indexPath.row] 
    cell.setCell(food.name, prepTimeText: food.time, imageName: (food.imageName)) 
    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 

    let iteam = arrayOfIteams[indexPath.row] 


    var detailViewController : DetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DetailViewController") as DetailViewController 
    detailViewController.nameStr = iteam.name 
    detailViewController.iteamPrepTime = iteam.time  // here I got an error 
    detailViewController.iteamImageName = iteam.imageName 
    self.presentViewController(detailViewController, animated: true, completion: nil) 

    } 

} 

其他nameStriteamImageName工作正常。

任何解決方案?

+0

嗯,是的:你試圖給一個UILabel類型的變量iteamPrepTime分配一個字符串team.time。那將是一個錯誤。你的意思是把它分配給UILabel的'text'屬性嗎? – 2014-10-27 09:05:56

+0

我做錯了嗎? – 2014-10-27 09:07:43

回答

2

你的變量稱爲「prepTime」,但您現在可直接分配到的UILabel itemPrepTime這還不可用(在視圖尚未加載)。您必須將值分配給您的變量,並將其分配給ViewDidLoad中的UILabel.text(如您已經這樣做)。

所以:

detailViewController.iteamPrepTime = iteam.time  // here I got an error 

應該是:

detailViewController.prepTime = iteam.time 

附加說明:試着在你的變量名一定的一致性。爲什麼在變量iteamImageName中包含iteam前綴?這會引起混淆,因爲UILabel插座也有這個前綴。只使用imageName作爲變量會更好。

+0

感謝它的工作,現在我犯了一個錯誤.. :) – 2014-10-27 10:16:39

0

我相信這是你需要做的

detailViewController.iteamPrepTime.text = iteam.time 

您已基本設定一個UILabel對象作爲一個String對象的價值是什麼。改爲設置UILabel的文本屬性。

讓我知道這對你的作品

+0

它給我錯誤如:致命錯誤:意外地發現零,而解包可選值 – 2014-10-27 09:40:30

+0

我試過了,我也試過iteamPrepTime?但是當它變成零時,沒有東西在標籤中的detailViewController上打印出來。 – 2014-10-27 09:43:57

+0

您的detailViewController是否已正確實例化?它是否具有非零值? – 2014-10-27 10:01:03

相關問題