![](/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.