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

标签 ios swift uiimageview uicollectionview uicollectionviewcell

我有一个 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/

相关文章:

ios - 我可以在计划提交给 Applestore 的代码中使用 C++ 吗? (iPhone产品)

ios - Xcode 4 图标问题!

ios - Swift 中的函数可以返回不同的数据类型吗?

ios - 为什么我的 UI 控件会根据模拟器的屏幕调整大小?

ios - TabBar/CollectionView 崩溃

ios - 如何将多个节点添加到场景并一次移动所有节点。 swift 。 SpriteKit

ios - 将 NSData 初始化为 nil SWIFT

ios - 我怎样才能看到 UIImageView 中的图像?

objective-c - 为什么我的 UIImageView 对象没有实时绘制?

ios - 如何在 MKMapView 中显示 UIImage