Reputation: 133
I want to be able to change the definition of my cell to read off different custom cell classes. I have tried referring to this post and reading the chat, but to no avail. Here is my code for defining the cell variable
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DiscussionTableViewCell else {fatalError("Unable to create cell")}
I also referred to this post to potentially have a variable equal the class that is changing using this code
let customCell = DiscussionTableViewCell.self
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? customCell else {fatalError("Unable to create cell")}
However I get an error 'Use of undeclared type 'customCell''. I don't mind if the solution is changing a variable to refer to the right class or just have the change inside an if statement, but no solutions have tried actually worked. It would be nice if I could simply do something like this:
if (singlePageVar == 2) {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DiscussionTableViewCell else {fatalError("Unable to create cell")}
cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
}
if (singlePageVar == 25) {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DiscussionTableViewCell else {fatalError("Unable to create cell")}
//assign different things here
}
return cell
but no matter how I define cell above this code such as let cell: UITableViewCell and then change it in the if statement there is always the same error.
Upvotes: 1
Views: 498
Reputation: 285069
If you have the same custom cell type but different content create the cell before the first if
statement.
And force downcast the cell, it has the same effect as the guard / fatalError
syntax.
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DiscussionTableViewCell
if singlePageVar == 2 {
cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
}
else if singlePageVar == 25 {
//assign different things here
}
return cell
However if you have different cell types you have to create and return the cells separately
if (singlePageVar == 2) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DiscussionTableViewCell cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
return cell
}
else if (singlePageVar == 25) {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath) as! OtherTableViewCellelse
//assign different things here
return cell
}
return UITableViewCell()
And once again:
Upvotes: 1
Reputation: 1984
You can use like this:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DiscussionTableViewCell
if (singlePageVar == 2) {
cell.discussionTitleLabel.text = discussionTitles[indexPath.row]
cell.discussionDescriptionLabel.text = discussionDescriptions[indexPath.row]
}
if (singlePageVar == 25) {
//assign different things here
}
return cell
Upvotes: 0