我有问题,使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