Reputation: 13
I have an assignment where I should create a product list in view controller with table view and custom cells. I customized the cell and I'm getting a error while compiling.
class ElectronicsTableViewCell: UITableViewCell {
@IBOutlet weak var productNameLabel: UILabel!
@IBOutlet weak var buyLabel: UILabel!
@IBOutlet weak var primeLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
}
class ProductListViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView:UITableView!
var electronicProducts = ["Swift DVD","Swift Manual","Beats EarPhones","IPad","I Watch"]
var price = ["45","34","67","90","89"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return electronicProducts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell",for: indexPath as IndexPath) as! ElectronicsTableViewCell
let productName = electronicProducts [indexPath.row]
cell.productNameLabel?.text = productName
cell.buyLabel?.text = "Buy"
cell.primeLabel?.text = "Prime Eligible"
let productPrice = price [indexPath.row]
cell.priceLabel?.text = productPrice
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("you selected cell \(indexPath.row)")
}
}
The error is : Reading from private effective user settings. Could not cast value of type 'UITableViewCell' (0x10300f858) to 'assignment2.ElectronicsTableViewCell' (0x1013b3178). (lldb)
Upvotes: 1
Views: 1164
Reputation: 346
Assuming you have marked the class for the TableViewCell in the storyboard, is this by any chance a duplicate of this question Also avoid using force casts. Try
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell",for: indexPath as IndexPath)
if let labelCell = cell as? ElectronicsTableViewCell {
let productName = electronicProducts [indexPath.row]
labelCell.productNameLabel?.text = productName
labelCell.buyLabel?.text = "Buy"
labelCell.primeLabel?.text = "Prime Eligible"
let productPrice = price [indexPath.row]
labelCell.priceLabel?.text = productPrice
return labelCell
}
return cell
}
Upvotes: 1
Reputation: 2321
I think the main issue is with the line:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")
You need to register your custom class, so change UITableViewCell to ElectronicsTableViewCell.
Hope that helps.
Upvotes: 4