【问题标题】:How to connect custom cells to UIViewController?如何将自定义单元格连接到 UIViewController?
【发布时间】: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)

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我认为主要问题在于这条线:

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")
    

    您需要注册您的自定义类,因此将 UITableViewCell 更改为 ElectronicsTableViewCell。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      假设您已在情节提要中标记了 TableViewCell 的类,这是否与 this question 重复 还要避免使用强制施法。试试

      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
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 2018-02-19
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多