如何在swift 3中的集合视图控制器中使用自定义单元格 [英] how to use custom cell in collection view controller in swift 3

查看:90
本文介绍了如何在swift 3中的集合视图控制器中使用自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using swift 3 - I know how to show some Images in collection view with custom Cell - the problem is that I can't use custom cell in Collection View Controller

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

here is the collection view Controller Codes private let reuseIdentifier = "uploadCell"

class uploadedFiles:UICollectionViewController {

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
}

这里是收藏查看单元格代码

and here is the Collection view Cell Code

   import UIKit

  class UICollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellImages: UIImageView!
   }

记得我用uploadCell作为标识符

remember that I used "uploadCell" for identifier

推荐答案

使用CollectionView自定义单元格

For using CollectionView Custom Cell

步骤1:创建CollectionViewCell的子类

Step 1: Create Subclass of CollectionViewCell

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

第2步:在StoryBoard中设置CollectionViewCell类

Step 2: set CollectionViewCell class in StoryBoard

第3步:reuseCollectionView Cell

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

这篇关于如何在swift 3中的集合视图控制器中使用自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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