2017-02-20 37 views
0

我在ViewController.Now是新来的迅速和努力实现的CollectionView功能我有三种UICollectionViewCell的,这里是我的代码:?如何使用switch语句在Swift中实现collectionView函数?

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

    if collectionView == self.collectionViewCategory { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell 

     cell.CategoryIcon.image = self.categoryItems[indexPath.item] 
     cell.layer.borderColor = UIColor.lightGray.cgColor 
     cell.layer.borderWidth = 1 
     cell.layer.cornerRadius = 5 

     return cell 
    } 
    else if collectionView == self.collectionViewHour { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell 

     cell.hourItem.text = self.hourItems[indexPath.item] 
     cell.layer.borderColor = UIColor.lightGray.cgColor 
     cell.layer.borderWidth = 1 
     cell.layer.cornerRadius = 5 

     return cell 
    } 
    else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: minuteIdentifier, for: indexPath as IndexPath) as! MinuteCell 

     cell.minuteItem.text = self.minuteItems[indexPath.item] 
     cell.layer.borderColor = UIColor.lightGray.cgColor 
     cell.layer.borderWidth = 1 
     cell.layer.cornerRadius = 5 

     return cell 
    } 
} 

我可以使用switch语句来达到同样的效果做的代码看起来更优雅?

我希望我的代码如下所示:

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

    let 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 
} 

但我得到了很多错误,我不知道如何解决这些问题。

+0

添加枚举所有可能的情况。 –

+0

谢谢!我会尽力的。 –

回答

0

希望这有助于。根本不测试。如果有任何问题,你可以采取剪断?

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 

}

+0

Thx很多!几乎完成了。现在我必须在默认情况下添加一个“break”,并且我得到编译器说在switch语句的“初始化之前使用的常量”单元格。我可以定义一个空的UICollectionViewCell来解决这个问题吗? –

+0

是的。只要确保你的单元格在默认情况下将被初始化。 –

2

您可以尝试分配给每个的CollectionView一个独特的标签,像这样

collectionView.tag = 1 

,然后使用标签作为标识符:

switch collectionView.tag { 
    case 0: 
     // some code 
    case 1: 
     // some code 
    default: 
     // some code 
} 

如果您使用内UITableViewCell是你collectionViews你可以将每个collectionView.tag设置为indexPath.sectionindexPath.row内的tableView(_ tableView: willDisplay cell: forRowAt indexPath:)

这里是一个不错的子类,你可以使用:

class CollectionViewTableViewCell: UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 

    func setCollectionViewDataSourceDelegate(dataSource: UICollectionViewDataSource?, dataDelegate: UICollectionViewDelegate?, forSection section: Int) { 

     collectionView.delegate = dataSource as! UICollectionViewDelegate? 
     collectionView.dataSource = dataDelegate as! UICollectionViewDataSource? 
     collectionView.tag = section 
     collectionView.reloadData() 
    } 
} 

,然后在您的视图控制器实现

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    guard let tableViewCell = cell as? CollectionViewTableViewCell else { return } 
    tableViewCell.setCollectionViewDataSourceDelegate(dataSource: self, dataDelegate: self, forSection: indexPath.section) 
} 
+0

Swift不需要那些“休息”。 – vacawama

+0

你是绝对正确的,正如官方[文档](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html)。我编辑了答案,谢谢!我不知道。我想这只是我过去的自己来自其他语言等:) :) –