I'm trying to move my "cell" code from the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)" function to its own class. I have registered the forCellReuseIdentifier and class with the ViewController (I think). And I have set the reuseIdentifier for the prototype cell to "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:
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
If that means a prototype cell in a storyboard, then delete this line:
MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")
That line means: Do not use the storyboard prototype. So you will need to delete it.
You will also need to set the class of the prototype cell in the storyboard.
However, I would then expect your app to crash because your init(coder:)
implementation says to crash.
fatalError("init(coder:) has not been implemented")
You'll probably want to fix that too...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.