在Collection视图的单元格中嵌入数组中的图像

时间:2014-10-03 22:08:21

标签: ios swift uiimageview uicollectionview uicollectionviewcell

我有一个UIViewController设置,里面还包含一个UICollectionView。此Collection视图有多个单元格,我希望每个单元格显示一个图像。图像包含在名为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视图没有创建单元格?

谢谢, 亚洲时报Siddharth

1 个答案:

答案 0 :(得分:0)

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

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