2017-07-19 68 views
0

我想爲我的按鈕使用集合視圖。所以,在故事板中,我拍了8個按鈕並將其添加到收藏視圖中。收集視圖具有8個單元格,每個按鈕具有8個唯一標識符。當我將它添加到集合視圖中時,所有按鈕都會顯示。拖動按鈕到CollectionView(Xcode 8)

但我的問題是,當我建立和運行,我沒有得到任何錯誤,但單元格中的8個按鈕根本不顯示!

我沒有在後臺做任何代碼。只需拖放和使用屬性。

所以我有點困惑,爲什麼它沒有顯示。我希望得到一些指導。

+0

您是否控制從集合視圖拖動到vc以設置委託和數據源?你也應該讓你的vc符合集合視圖委託方法。 – Douglas

+0

@Douglas:是的,我做到了。我做了更多的研究,但我確實忘了這麼做,所以謝謝!但我有多個具有不同標識符的單元格(我不能使用一個標識符),因此它現在只顯示一個單元格。 – insolence

+0

在您的cellforindexpath方法中,您最有可能指定了一個重用標識符。如果您有8種不同的細胞類型,則需要確定訂單並使用if語句。如果索引路徑爲1,則使用標識符爲「firstcell」的單元。如果索引路徑是2,則使用另一個等等。 – Douglas

回答

1

讓我繼續使用@ Douglas的評論來回答這個問題。所以,我完全忘了做第一件事是CTRL +拖動集合視圖的ViewController在故事板,使數據源委託。然後,在我的ViewController.swift中,我實現了以下代碼:

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

    if(indexPath.item == 0) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "kitchenButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 1) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 2) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "eventsButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 3) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mapButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 4) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "journeyButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 5) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "callButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 6) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "handsButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 7) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "swimButton", for: indexPath) 
     return cell 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "none", for: indexPath) 
      return cell 
    } 


} 

以下代碼適用於多個單元格。我正在嘗試實現水平滾動效果,所以此方法對我來說非常合適。

確保包含UICollectionViewDelegateFlowLayout,UICollectionViewDataSource進入類的標題。

謝謝@Douglas!

+0

走吧!很高興聽到你的工作! – Douglas