拖动按钮到CollectionView(Xcode 8)

问题描述:

我想为我的按钮使用集合视图。所以,在故事板中,我拍了8个按钮并将其添加到收藏视图中。收集视图具有8个单元格,每个按钮具有8个唯一标识符。当我将它添加到集合视图中时,所有按钮都会显示。拖动按钮到CollectionView(Xcode 8)

但我的问题是,当我建立和运行,我没有得到任何错误,但单元格中的8个按钮根本不显示!

我没有在后台做任何代码。只需拖放和使用属性。

所以我有点困惑,为什么它没有显示。我希望得到一些指导。

+0

您是否控制从集合视图拖动到vc以设置委托和数据源?你也应该让你的vc符合集合视图委托方法。 – Douglas

+0

@Douglas:是的,我做到了。我做了更多的研究,但我确实忘了这么做,所以谢谢!但我有多个具有不同标识符的单元格(我不能使用一个标识符),因此它现在只显示一个单元格。 – insolence

+0

在您的cellforindexpath方法中,您最有可能指定了一个重用标识符。如果您有8种不同的细胞类型,则需要确定订单并使用if语句。如果索引路径为1,则使用标识符为“firstcell”的单元。如果索引路径是2,则使用另一个等等。 – Douglas

让我继续使用@ 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