简体   繁体   中英

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

I'm using two TableViews in a ViewController but I get this error when it gets to the second TableViewCell , cartProductCell . They both have custom classes, and outlets, as it was the problem for many in other posts. Is the first time I'm doing this and I can't find a solution for this. May it be just because I'm using custom classes for the cells? In the tutorials I found about two TableViews weren't used custom classes.

In the Storyboard editor everything is connected well and identifiers are both correct. Here's the function:

    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!

    }

As usual many thanks

After a few tries and after fixing a silly mistake I finally made it work by assigning the value of custom cells to cell and the function's code is now:

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!

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM