gpt4 book ai didi

ios - Swift:在没有 Collection View Controller 的情况下实现 Collection View

转载 作者:搜寻专家 更新时间:2023-10-31 08:34:10 25 4
gpt4 key购买 nike

我设置了一个 CollectionViewController 并让一切都运行得很好而且很直接。

class CollectionController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
.
.
.
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
cell.myTitle.text = self.titles[indexPath.row]
let imgName = "pic\(indexPath.row).jpg"
cell.myImage.image = UIImage(named: imgName)

return cell
}
}
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myTitle: UILabel!
@IBOutlet weak var myImage: UIImageView!
}

现在我只想对 UICollectionView(不是 UICollectionViewController)做同样的事情。我以同样的方式实现了我的 Collection,但是当我运行我的应用程序时,没有渲染任何单元格。

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class CollectionView: UICollectionView, UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
cell.myTitle.text = self.titles[indexPath.row]
let imgName = "pic\(indexPath.row).jpg"
cell.myImage.image = UIImage(named: imgName)

return cell
}
}
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myTitle: UILabel!
@IBOutlet weak var myImage: UIImageView!
}

我需要在我的 ViewController 中实现一些初始化吗?我想我在这里遗漏了一些小东西 :(

*我正在使用 Storyboard,所以我的单元格等是在那里生成的,而不是通过代码生成的。

最佳答案

您的 View Controller 是否设置为 Collection View 的委托(delegate)和数据源?

您可以通过 collectionView.delegate = self 之类的代码或通过在 View Controller 和 Collection View 之间按住 control 并拖动从 Storyboard 中执行此操作。

关于ios - Swift:在没有 Collection View Controller 的情况下实现 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com