我无法在CollectionView中显示3个单元格

时间:2019-04-29 09:17:24

标签: ios swift

switch indexPath.row {
        case 0:
            let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell", for: indexPath)
             return cell
        case 1:
            let cell2 = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell2", for: indexPath)
            return cell2
        default:
            let cell3 = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell3", for: indexPath)
            return cell3
}

2 个答案:

答案 0 :(得分:1)

  

UICollectionViewDelegateFlowLayout

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
 {
    let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell", for: indexPath)
    return cell
}
  

商品数量

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
{
  return 3
}
  

用于连续获取3个单元格

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
    {
       let yourWidth = collectionView.bounds.width/3.0
       let yourHeight = yourWidth

       return CGSize(width: yourWidth, height: yourHeight)
    }

答案 1 :(得分:0)

我不明白您到底想要什么,但是尝试一下:

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

    let cell = UICollectionViewCell()
    switch indexPath.row {
        case 0:
            let cell1 = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell", for: indexPath)
             cell = cell1
             break
        case 1:
            let cell2 = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell2", for: indexPath)
            cell = cell2
             break
        default:
            let cell3 = self.collectionView.dequeueReusableCell(withReuseIdentifier: "formCell3", for: indexPath)
            cell = cell3
            break
    }
    return cell
}