在一个ViewController中使用自定义单元创建多个UICollectionViews

时间:2018-10-28 08:58:30

标签: swift xcode uicollectionview

我想在一个ViewController中添加三个UICollectionViews。我希望其中两个具有自定义单元格。如果CollectionViews没有自定义单元格将没有问题。

这将是我针对没有自定义单元格的解决方案:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell: UICollectionViewCell?


        if collectionView == gamePad{
            cell = gamePad.dequeueReusableCell(withReuseIdentifier: "coloredCell", for: indexPath)
            cell?.backgroundColor = UIColor.gray
            cell?.layer.cornerRadius = (cell?.frame.height)!/2

        }
        if collectionView == blackCounterCV {
            cell = blackCounterCV.dequeueReusableCell(withReuseIdentifier: "blackC", for: indexPath)
            cell?.backgroundColor = UIColor.black
            cell?.layer.cornerRadius = (cell?.frame.height)!/2    
        }
        if collectionView == whiteCounterCV {
            cell = whiteCounterCV.dequeueReusableCell(withReuseIdentifier: "whiteC", for: indexPath)
            cell?.backgroundColor = UIColor.white
            cell?.layer.cornerRadius = (cell?.frame.height)!/2

        }
        return cell!
    }

我有一个标签,是在一个单独的文件中创建的。现在我看到,必须将单元格强制转换为UICollectionViewCell的类。

那是我的主意,没用:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell: UICollectionViewCell?


        if collectionView == gamePad{
            cell = gamePad.dequeueReusableCell(withReuseIdentifier: "coloredCell", for: indexPath)
            cell?.backgroundColor = UIColor.gray
            cell?.layer.cornerRadius = (cell?.frame.height)!/2

        }
        if collectionView == blackCounterCV {
            cell = blackCounterCV.dequeueReusableCell(withReuseIdentifier: "blackC", for: indexPath) as! BlackCCollectionViewCell
            cell?.backgroundColor = UIColor.black
            cell?.layer.cornerRadius = (cell?.frame.height)!/2
            cell.blackLbl.text = "TEST"
        }
        if collectionView == whiteCounterCV {
            cell = whiteCounterCV.dequeueReusableCell(withReuseIdentifier: "whiteC", for: indexPath) as! WhiteCCollectionViewCell
            cell?.backgroundColor = UIColor.white
            cell?.layer.cornerRadius = (cell?.frame.height)!/2
            cell.whiteLbl.text = "TEST"

        }
        return cell!
    }

错误,显示为:

  

“ UICollectionViewCell”类型的值?没有成员'blackLbl'/'whiteLbl'

因此,我认为演员表无法按我的意愿工作。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

  

“ UICollectionViewCell”类型的值?没有成员'blackLbl'/   'whiteLbl'

到目前为止,您的代码似乎正确,问题可能是InterfaceBuilder中缺少的内容。

因此,此问题可能是单元格中缺少缺少的Class名称。

确保已将类自定义类型添加到IB

的单元格中

例如情节提要上的BlackCCollectionViewCell,请确保正确的单元格具有此类。 Here

更新:由于这是编译错误,请尝试以下代码。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == gamePad{
        var cell: UICollectionViewCell?
        cell = gamePad.dequeueReusableCell(withReuseIdentifier: "coloredCell", for: indexPath)
        cell?.backgroundColor = UIColor.gray
        cell?.layer.cornerRadius = (cell?.frame.height)!/2
        return cell!
    }
    if collectionView == blackCounterCV {
       let cell = blackCounterCV.dequeueReusableCell(withReuseIdentifier: "blackC", for: indexPath) as! BlackCCollectionViewCell
        cell.backgroundColor = UIColor.black
        cell.layer.cornerRadius = (cell.frame.height)/2
        cell.blackLbl.text = "TEST"
        return cell
    }
    if collectionView == whiteCounterCV {
        let cell = whiteCounterCV.dequeueReusableCell(withReuseIdentifier: "whiteC", for: indexPath) as! WhiteCCollectionViewCell
        cell.backgroundColor = UIColor.white
        cell.layer.cornerRadius = (cell.frame.height)/2
        cell.whiteLbl.text = "TEST"
        return cell
    }
        return UICollectionViewCell()
}