2017-03-02 31 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