正确的方法将数组中的图像迭代并加载到Swift中的CollectionViewController中 [英] Proper Way to Iterate and Load Images from Array into CollectionViewController in Swift

查看:78
本文介绍了正确的方法将数组中的图像迭代并加载到Swift中的CollectionViewController中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XCode 6和iOS 8在Swift中开发应用程序。这个应用程序包含一个我想要加载图像数组的集合视图。

I'm working on an app in Swift using XCode 6 and iOS 8. This app contains a collection view that I'd like to load an array of images into.

当我只使用一个图像时,我可以按照我的意愿重复多次,但是当遍历数组时,只重复最后一个图像,而不是出现在集合视图中的唯一图像。

When I'm using just one image I can repeat it as many times as I'd like, but when iterating through the array only the last image is repeated vs the unique images appearing in the collection view.

我的数组在我的类中被定义为:

My array is defined as this inside my class:

var listOfImages: [UIImage] = [
    UIImage(named: "4x4200.png")!,
    UIImage(named: "alligator200.png")!,
    UIImage(named: "artificialfly200.png")!,
    UIImage(named: "baitcasting200.png")!,
    UIImage(named: "bassboat200.png")!,
    UIImage(named: "bighornsheep200.png")!,
    UIImage(named: "bison200.png")!,
    UIImage(named: "blackbear200.png")!,
    UIImage(named: "browntrout200.png")!
]

接下来我有以下内容来遍历数组并显示图像:

Next I have the following to iterate through the array and display the images:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell


    // Configure the cell
    for images in listOfImages{
      cell.imageView.image = images
    }

    return cell
}

此编译并显示但仅显示browntrout200.png。我缺少什么来显示所有图像?

This compiles and displays but is only displaying browntrout200.png. What am I missing to display all the images?

推荐答案

正在发生的事情是永远的集合视图单元格,你正在迭代你的数组并将单元格的图像设置为数组中的每个图像。数组中的最后一个图像是browntrout200.png,这是您看到的唯一图像。您需要使用indexPath来获取数组中的单个图像。

What is happening is for ever collection view cell, you are iterating through your array and setting the image of the cell to every image in your array. The last image in your array is "browntrout200.png" and that is the only one you see. You need to use the indexPath to get a single image in your array.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
    cell.imageView.image =  listOfImages[indexPath.row]

    return cell
}

此外,请确保您设置了另一个UICollectionViewDataSource方法,以返回listOfImages数组中的项目数。

Also, make sure you have the other UICollectionViewDataSource method set tor return the number of items in your listOfImages array.

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return listOfImages.count
}

这篇关于正确的方法将数组中的图像迭代并加载到Swift中的CollectionViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆