2014-10-02 39 views
1

我有我的故事板2個UICollectionView,每個具有其自己的出口:兩個UICollectionViews在一個UIViewController中2個UICollectionViewCells

@IBOutlet weak var daysCollectionView: UICollectionView! 
@IBOutlet weak var hoursCollectionView: UICollectionView! 

在每一集的看法,我想用一個不同類型的細胞。所以我創建了一個DayCell類和一個HourCell類。

然後在cellForItemAtIndexPath:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
{ 
    if collectionView == self.dayCollectionView 
    { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("dayCell", forIndexPath: indexPath) as DayCell 
     ... 
     return cell 
    } 
    else if collectionView == self.hourCollectionView 
    { 
     let cell: HourCell = collectionView.dequeueReusableCellWithReuseIdentifier("hourCell", forIndexPath: indexPath) as HourCell 
     ... 
    return cell 
    } 
} 

我得到一個編譯器錯誤

失蹤有望迴歸UITableCellView」函數返回。

我完全失去了一些東西或在if聲明中的回報在這種情況下不起作用嗎?

或者我只是在談論這個完全錯誤的?這似乎是每個人都在建議的答案。我只是無法讓它工作。

回答

3

這是因爲代碼中的if條件不是'詳盡的',即存在執行可能到達函數末尾而無法返回單元的情況。 (例如,您可能會在今後引進額外的集合視圖)

這裏有一個簡單的解決辦法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if collectionView == self.dayCollectionView { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("dayCell", forIndexPath: indexPath) as DayCell 
     ... 
     return cell 
    } else { // do not do this check: if collectionView == self.hourCollectionView { 
     let cell: HourCell = collectionView.dequeueReusableCellWithReuseIdentifier("hourCell", forIndexPath: indexPath) as HourCell 
     ... 
     return cell 
    } 
} 
+0

這不僅解決我的問題,但我覺得學歷的爲好。謝謝! – fedoroffn 2014-10-02 18:45:33

相關問題