簡體   English   中英

無法在兩個 TableView 視圖控制器 Swift4 中使帶有標識符的單元格出列

[英]unable to dequeue a cell with identifier in a two TableView View Controller Swift4

我在ViewController使用了兩個TableViews ,但是當它到達第二個TableViewCell時出現此錯誤, cartProductCell 他們都有自定義類和插座,因為這是其他帖子中許多人的問題。 這是我第一次這樣做,我找不到解決方案。 可能只是因為我為單元格使用了自定義類? 在教程中,我發現有兩個 TableView 沒有使用自定義類。

在 Storyboard 編輯器中,一切都連接良好,標識符都正確。 這是函數:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell:UITableViewCell?

//        if tableView == self.worksTableView && CartViewController.bookedWoksArray.count > 0 {
        if tableView == self.worksTableView  {

            let cell = tableView.dequeueReusableCell(withIdentifier: "cartWorkCell", for: indexPath as IndexPath) as! CartWorkTableViewCell
            cell.workImageView.image = CartViewController.bookedWoksArray[indexPath.row].0
            cell.workNameLabel.text = CartViewController.bookedProductsArray[indexPath.row].1
            cell.workPriceLabel.text = CartViewController.bookedWoksArray[indexPath.row].2

        } // else {return}

//         else if tableView == self.worksTableView && CartViewController.bookedProductsArray.count > 0 {
        if tableView == self.worksTableView {


            let cell = tableView.dequeueReusableCell(withIdentifier: "cartProductCell", for: indexPath as IndexPath) as! CartProductTableViewCell

            cell.cartProductImageView.image = CartViewController.bookedProductsArray[indexPath.row].0
            cell.cartProductNameLabel.text = CartViewController.bookedProductsArray[indexPath.row].1
            cell.cartProductPriceLabel.text = CartViewController.bookedProductsArray[indexPath.row].2
        } //else {return}
        return cell!

    }

像往常一樣非常感謝

經過幾次嘗試並修復了一個愚蠢的錯誤后,我終於通過將自定義單元cell的值分配給單元格來使其工作,函數的代碼現在是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell:UITableViewCell?

        if tableView == self.worksTableView  {

            let workCell = tableView.dequeueReusableCell(withIdentifier: "cartWorkCell", for: indexPath) as! CartWorkTableViewCell
            workCell.workImageView.image = CartViewController.bookedWoksArray[indexPath.row].0
            workCell.workNameLabel.text = CartViewController.bookedWoksArray[indexPath.row].1
            workCell.workPriceLabel.text = CartViewController.bookedWoksArray[indexPath.row].2
            cell = workCell
        } 

        if tableView == self.productsTableView{


            let productCell = tableView.dequeueReusableCell(withIdentifier: "cartProductCell", for: indexPath) as! CartProductTableViewCell

            productCell.cartProductImageView.image = CartViewController.bookedProductsArray[indexPath.row].0
            productCell.cartProductNameLabel.text = CartViewController.bookedProductsArray[indexPath.row].1
            productCell.cartProductPriceLabel.text = CartViewController.bookedProductsArray[indexPath.row].2
            cell = productCell
        }
        return cell!

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM