繁体   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