【发布时间】:2017-09-15 17:54:42
【问题描述】:
我有一个任务,我应该在视图控制器中创建一个带有表格视图和自定义单元格的产品列表。我自定义了单元格,但在编译时出现错误。
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)")
}
}
错误是: 从私人有效用户设置中读取。 无法将“UITableViewCell”(0x10300f858)类型的值转换为“assignment2.ElectronicsTableViewCell”(0x1013b3178)。 (lldb)
【问题讨论】: