我開發一個簡單的tableView
與Custom Cell
並在當任一行用戶點擊它定位到下一個DetailViewController
和detailViewController
顯示一個信息,我的代碼的子類型是:「的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)
}
}
其他nameStr
和iteamImageName
工作正常。
任何解決方案?
嗯,是的:你試圖給一個UILabel類型的變量iteamPrepTime分配一個字符串team.time。那將是一個錯誤。你的意思是把它分配給UILabel的'text'屬性嗎? – 2014-10-27 09:05:56
我做錯了嗎? – 2014-10-27 09:07:43