我有問題,使cellForRowAt工作,但不幸的是我的應用程序編譯後仍然空白。我使用的Xcode 8.3測試版3Swift cellForRowAt不顯示
import UIKit
class myCell: UITableViewCell {
@IBOutlet var myLabel: UILabel!
@IBOutlet var myImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我創建了一個食品類,使工作更輕鬆:
import Foundation
class Food {
var imageName = ""
var description = ""
var moreInfo = ""
init(imageName: String, description: String, moreInfo: String) {
self.imageName = imageName
self.description = description
self.moreInfo = moreInfo
}
}
最後,這是TableViewController:
import UIKit
class TableViewController: UITableViewController {
var foodArray: [Food] = [Food]()
override func viewDidLoad() {
super.viewDidLoad()
let food1 = Food(imageName: "cake.jpg", description: "Chocolate Cake", moreInfo: "There is nothing better than a great chocolate cake.")
let food2 = Food(imageName: "meringue.jpg", description: "Meringue & Berries", moreInfo: "I really don't like meringue but it's a nice photo.")
let food3 = Food(imageName: "peaches.jpg", description: "Grilled Peaches", moreInfo: "This would be perfect as a summer time dessert.")
let food4 = Food(imageName: "tiramisu.jpg", description: "Tiramisu", moreInfo: "One of my favorite Italian desserts. Yum.")
foodArray.append(food1)
foodArray.append(food2)
foodArray.append(food3)
foodArray.append(food4)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foodArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell
let foodItem = foodArray[indexPath.row]
cell.myImageView.image = UIImage(named: foodItem.imageName)
cell.myLabel.text = foodItem.description
self.tableView.reloadData()
return cell
}
}
任何人都可以請讓我看看我做錯了什麼?
該方法是否解僱?或者你說整個屏幕是空白的?因爲後者是一個不同的問題。 – dylanthelion
應用程序是空的 – Virtuoso
我會採取@reinier Melian的建議,除了完全刪除每個reloadData()調用。讓我知道這是否有效。 reloadData()調用你的表視圖的所有數據源方法,你可能在這裏有一個循環。另外,爲了將來的參考,UI更新應該在主線程上運行。 – dylanthelion