我在使cellForRowAt工作时遇到问题,但不幸的是我的应用程序在编译后仍然是空白的。我正在使用Xcode 8.3 beta 3。
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
}
}
我创建了一个Food类来简化工作:
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
}
}
有谁能告诉我我做错了什么?
答案 0 :(得分:1)
您不应在self.tableView.reloadData()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
self.tableView.reloadData()
替换此代码的viewDidLoad方法
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)
}
并将此代码用于func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
代码
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
return cell
}
我希望这有助于你
答案 1 :(得分:1)
您需要注册该类并使用以下方法添加重用标识符:
tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:“Cell”)
在viewDidLoad
中添加此方法此外,您不需要在cellForRowAtIndexPath方法中调用self.tableView.reloadData()。
希望有所帮助
答案 2 :(得分:0)
问题是您正在调用reloadData()
以便解决此问题,请将其从代码中删除:
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
return cell
}