2017-02-20 59 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)。我編輯了答案,謝謝!我不知道。我想這只是我過去的自己來自其他語言等:) :) –