如何在swift 3

时间:2017-05-30 08:39:10

标签: ios image swift3 uicollectionview uicollectionviewcell

我正在使用swift 3 - 我知道如何使用自定义Cell在集合视图中显示一些图像 - 问题是我无法在集合视图控制器

这里是集合视图控制器代码 private let reuseIdentifier =" uploadCell"

class uploadedFiles:UICollectionViewController {

var images = ["1.jpg" , "2.jpg" , "3.jpg" , "4.jpg"  ]

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    //myCollectionView.sizeToFit()
    //cell.sizeToFit()


    cell.cellImages.image = UIImage(named: images[indexPath.row])



    return cell
}

这里是Collection视图Cell Code

   import UIKit

  class UICollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellImages: UIImageView!
   }

记得我用过" uploadCell"标识符

2 个答案:

答案 0 :(得分:5)

使用CollectionView自定义单元格

步骤1:创建CollectionViewCell的子类

class ImageCell: UICollectionViewCell {
    @IBOutlet weak var imgView: UIImageView!
}

第2步:在StoryBoard中设置CollectionViewCell类

第3步:reuseCollectionView Cell

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell

    cell.imgView.image = self.arrMedia[indexPath.row] as? UIImage
    return cell
}

答案 1 :(得分:0)

UICollectionView实现非常有趣。您可以从以下链接中获得非常简单的源代码和视频教程:

https://github.com/Ady901/Demo02CollectionView.git

https://www.youtube.com/watch?v=5SrgvZF67Yw

class DummyCollectionCell: UICollectionViewCell {

    @IBOutlet weak var userImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DummyCollectionCell", for: indexPath) as! DummyCollectionCell
                cell.titleLabel.text = nameArr[indexPath.row]
                cell.userImageView.backgroundColor = .blue
                return cell
}