gpt4 book ai didi

ios - 将数组中的图像嵌入到 Collection View 的单元格中

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:55 26 4
gpt4 key购买 nike

我有一个 UIViewController 设置,其中还包含一个 UICollectionView。这个 Collection View 有多个单元格,我想让每个单元格显示一个图像。这些图像包含在一个名为 imageThumbsAray 的数组中。每个单元格都是一个 90x90 的正方形,我希望图像填充每个单元格的内容区域。下面是我的 Collection View 代码。这似乎是正确的,但是当我运行该应用程序时,返回的是一个空的 Collection View 。

func SetupCollectionView() {
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 90)
PastObjectsCollection = UICollectionView(frame: CGRect(x: 16, y: 229, width: 368, height: 368), collectionViewLayout: layout)
PastObjectsCollection!.dataSource = self
PastObjectsCollection!.delegate = self
PastObjectsCollection!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier:"Chow Object Reuse ID")

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objectsCount
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Chow Object Reuse ID", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.whiteColor()
var imageView:UIImageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0, width: 90, height: 90)
imageView.image = imageThumbsArray[indexPath.row]
cell.addSubview(imageView)
return cell
}

为什么 Collection View 没有创建单元格?

谢谢,悉达多

最佳答案

此代码尽可能多地使用了您的代码。你可以逐行比较。我没有您的图像 Assets 或对象数组。这都是程序化的。如果您需要更多帮助,请告诉我。

import UIKit


class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var PastObjectsCollection:UICollectionView?
var layout:UICollectionViewFlowLayout?

override func viewDidLoad() {
super.viewDidLoad()


SetupCollectionView()
}


func SetupCollectionView() {
self.layout = UICollectionViewFlowLayout()
self.layout!.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
self.layout!.itemSize = CGSize(width: 90, height: 90)
self.PastObjectsCollection = UICollectionView(frame: CGRect(x: 16, y: 229, width: 368, height: 368), collectionViewLayout: layout!)
self.PastObjectsCollection!.dataSource = self
self.PastObjectsCollection!.delegate = self
self.PastObjectsCollection!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier:"Chow Object Reuse ID")
self.PastObjectsCollection!.backgroundColor = UIColor.blackColor()
self.view.addSubview(self.PastObjectsCollection!)

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1 //objectsCount
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Chow Object Reuse ID", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.whiteColor()
var imageView:UIImageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0, width: 90, height: 90)
//imageView.image = imageThumbsArray[indexPath.row]
cell.addSubview(imageView)
return cell
}
}

关于ios - 将数组中的图像嵌入到 Collection View 的单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26187468/

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