2017-03-02 34 views
0

我在ViewController.Now是新來的迅速和努力實現的CollectionView功能我有三種UICollectionViewCell的,這裏是我的代碼:如何使用switch語句在Swift中實現collectionView函數?

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

    var cell:UICollectionViewCell 

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

我要聲明使用AnyObject變量細胞,但我得到了很多錯誤。更改爲UICollectionViewCell後,錯誤消失,然後仍然不起作用。

+0

開關indexPath.section或indexPath.row – 2017-03-02 03:07:44

+0

不要發表您自己的問題 –

+0

的副本做什麼你的意思是還沒有工作?你有錯誤還是其他? – chengsam

回答

0

您可以使用這樣的開關:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     switch indexPath.row { 
     case 0: 
      <#code#> 
     case 1: 
      <#code#> 
     default: 
      <#code#> 
     } 
    } 

,或者您可以使用indexPath.section