[英]I can't dequeueReusableCell when the Cell is in its own class
I'm trying to move my "cell" code from the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)" function to its own class.我正在尝试将我的“单元格”代码从“func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)”function 移动到它自己的 ZA2F2ED4F8EBC2CBB4C21A29DC04。 I have registered the forCellReuseIdentifier and class with the ViewController (I think).
我已经用 ViewController 注册了 forCellReuseIdentifier 和 class (我认为)。 And I have set the reuseIdentifier for the prototype cell to "MealSelector".
我已将原型单元格的重用标识符设置为“MealSelector”。 However, when I run the code, all of the cells are blank.
但是,当我运行代码时,所有单元格都是空白的。 Why is the data not populating the tables?
为什么数据没有填充表?
Here is the view controller:这是 controller 的视图:
import UIKit
class MealSelectorController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var MealTable: UITableView!
@IBOutlet weak var ConfirmOrderButton: UIButton!
var recipes: [[String?]] = []
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0 ..< 3 {
let recipe: Recipe = APICaller.getNewRecipe()
self.recipes.append([recipe.title, recipe.description])
}
MealTable.dataSource = self
MealTable.delegate = self
MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.recipes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MealSelector")
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 241
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
} }
Here is the cell:这是单元格:
import UIKit
class MealSelectorCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
} }
In a comment, you say:在评论中,你说:
It is not using my prototype cell at all to build the TableView
它根本没有使用我的原型单元来构建 TableView
If that means a prototype cell in a storyboard, then delete this line:如果这意味着 storyboard 中的原型单元,则删除此行:
MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")
That line means: Do not use the storyboard prototype.那条线的意思是:不要使用 storyboard 原型。 So you will need to delete it.
所以你需要删除它。
You will also need to set the class of the prototype cell in the storyboard.您还需要在 storyboard 中设置原型单元的 class。
However, I would then expect your app to crash because your init(coder:)
implementation says to crash.但是,我希望您的应用程序崩溃,因为您的
init(coder:)
实现说崩溃。
fatalError("init(coder:) has not been implemented")
You'll probably want to fix that too...你可能也想解决这个问题......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.