' NSString的'不是UILabel'的子类型。

时间:2014-10-27 09:01:03

标签: swift

我正在使用tableView开发一个简单的Custom Cell,当用户点击任意行时,它会导航到下一个DetailViewControllerdetailViewController会显示一个信息,而我的代码是:

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工作正常。

任何解决方案?

2 个答案:

答案 0 :(得分:2)

您的变量被称为' prepTime'但是您现在直接分配到尚未可用的UILabel itemPrepTime(视图尚未加载)。您必须将值赋给变量并将其分配给ViewDidLoad中的UILabel.text(如您所知)。

所以:

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

应该是:

detailViewController.prepTime = iteam.time

附加说明:尝试在变量名称中保持一致性。为什么在iteamImageName变量中包含iteam前缀?这会引起混淆,因为UILabel网点也有这个前缀。只使用imageName作为变量会更好。

答案 1 :(得分:0)

我相信这就是你需要做的事情

detailViewController.iteamPrepTime.text = iteam.time  

您基本上将UILabel对象的值设置为String对象。改为设置UILabel的text属性。

如果这对您有用,请告诉我