How can I use switch statement to implement collectionView function in Swift?

前端 未结 2 650
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 20:30

I am new to swift and trying to implement collectionView function in a ViewController.Now I have three types of UICollectionViewCell,here is my code:

func co         


        
2条回答
  •  Happy的楠姐
    2021-01-16 21:19

    Hope this help. Not testing at all. If any issues, can you take a snip ?

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
      let cell:UICollectionViewCell
    
      switch collectionView {
        case self.collectionViewCategory:
            let categoryCell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell
            categoryCell.CategoryIcon.image = self.categoryItems[indexPath.item]
            cell = categoryCell 
        case self.collectionViewHour:
            let hourCell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell
            hourCell.hourItem.text = self.hourItems[indexPath.item]
            cell = hourCell
        default:
            //do nothing.
      }
      cell.layer.borderColor = UIColor.lightGray.cgColor
      cell.layer.borderWidth = 1
      cell.layer.cornerRadius = 5
      return cell
    

    }

提交回复
热议问题