![](/img/trans.png)
[英]dequeueReusableCell problem. When I select one cell, another cells are being selected
[英]I can't dequeueReusableCell when the Cell is in its own class
我正在尝试将我的“单元格”代码从“func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)”function 移动到它自己的 ZA2F2ED4F8EBC2CBB4C21A29DC04。 我已经用 ViewController 注册了 forCellReuseIdentifier 和 class (我认为)。 我已将原型单元格的重用标识符设置为“MealSelector”。 但是,当我运行代码时,所有单元格都是空白的。 为什么数据没有填充表?
这是 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)
}
}
这是单元格:
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")
}
}
在评论中,你说:
它根本没有使用我的原型单元来构建 TableView
如果这意味着 storyboard 中的原型单元,则删除此行:
MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")
那条线的意思是:不要使用 storyboard 原型。 所以你需要删除它。
您还需要在 storyboard 中设置原型单元的 class。
但是,我希望您的应用程序崩溃,因为您的init(coder:)
实现说崩溃。
fatalError("init(coder:) has not been implemented")
你可能也想解决这个问题......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.