我正在尝试使用我的按钮的集合视图。所以,在故事板中我拿了8个按钮并将其添加到集合视图中。集合视图每个按钮有8个单元格,带有8个唯一标识符。当我将其添加到Collection View时,所有按钮都会显示。
但我的问题是,当我构建并运行时,我没有得到错误,但是单元格中的8个按钮根本没有显示!
我没有在后台执行任何代码。只需拖放即可使用属性。
所以我有点困惑为什么它没有显示。我很感激一些指导。
答案 0 :(得分:1)
让我继续使用@ Douglas的评论来回答这个问题。所以我完全忘记做的第一件事是按住Ctrl +拖动 集合视图到故事板中的 ViewController 并启用 dataSource < / em>和委托。然后在我的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!