【问题标题】:Embedding images from array in cells of Collection view将数组中的图像嵌入到集合视图的单元格中
【发布时间】:2014-10-03 22:08:20
【问题描述】:

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

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 视图没有创建单元格?

谢谢, 悉达多

【问题讨论】:

  • 您从未将收藏视图添加到您的视图中。
  • 我在 Interface Builder 中创建了一个集合视图,并作为 IBOutlet (PastObjectsCollection) 连接到代码。我还需要明确地将其添加到视图中吗?
  • 不,但是你为什么要在 SetupCollectionView() 函数中创建一个?您在 IB 中创建的集合视图中是否也有一个单元格?
  • 我正在尝试在代码中修改集合视图的属性,因此使用了 SetupCollectionView() 方法。在 IB 的集合视图中有一个单元格。但是,单元格的数量取决于imageThumbsArray 中的项目数量,这不是预先确定的
  • 好吧,那个代码是错误的。您需要通过其 IBOutlet 引用您在 IB 中创建的集合视图,而不是创建一个新的。

标签: ios swift uiimageview uicollectionview uicollectionviewcell


【解决方案1】:

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

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2021-01-31
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多