Reputation: 502
in my Xcode Project, I have the main storyboard
set up with a table view
and a prototype cell
with the identifier
set to "cell". The table view has a data source outlet
and a delegate outlet
.
Yet, my build crashes every time because of an "unresolved identifier" when I create the table view in line 11 (see code below).
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
let array = createArray()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(validDeck.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = UITableViewCell(style: UITableViewStyle.default, reuseIdentifier: "cell") |"ERROR: Unknown Identifier 'cell' "
cell.textlabel?.text = ""
return cell
}
Does anybody know this error and is willing to help?
Upvotes: 0
Views: 596
Reputation: 14841
You never initialize UITableViewCell
instances directly.
In viewDidLoad
:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
and then:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for indexPath: indexPath)
cell.textlabel?.text = ""
return(cell)
}
Upvotes: 1
Reputation: 9503
Use below code for swift 3.*
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return validDeck.count // assuming count must be greater then 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textlabel?.text = "Cell tittle"
return cell
}
Upvotes: 2