ios - 如何在 swift 3 的 Collection View Controller 中使用自定义单元格

标签 ios image swift3 uicollectionview uicollectionviewcell

我正在使用 swift 3 - 我知道如何使用自定义 Cell 在 Collection View 中显示一些图像 - 问题是我无法在 Collection View Controller 中使用自定义单元格

这里是 Collection View Controller 代码 private let reuseIdentifier = "uploadCell"

上传文件类: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 View 单元代码

   import UIKit

  class UICollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellImages: UIImageView!
   }

记住我使用“uploadCell”作为标识符

最佳答案

对于使用 CollectionView 自定义单元格

第 1 步:创建 CollectionViewCell 的子类

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

第二步:在 StoryBoard 中设置 CollectionViewCell 类

第三步:重用CollectionView 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
}

关于ios - 如何在 swift 3 的 Collection View Controller 中使用自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257423/

相关文章:

ios - 完成处理程序不返回任何内容,即使它看起来像在单步执行时一样

ios - 为 ios 构建时如何设置标志 -ObjC -all_load

ios - SwiftUI .popover 框架大小不正确

c++ - SDL2,创建比屏幕大的窗口

ios - 无法为 iOS 应用程序版本构建存档 - 命令/bin/sh 失败,退出代码为 1

ios - 如何在 iOS 上获取当前信号处理程序?

image - 如何更改 UIImageView 中 UIImage 的位置

php - php 中 getimagesize() 的有效替代方法

ios - 使用 .sks 文件布局复杂对象与 SKScene 文件

swift - 使用 macOS 应用程序运送 aws-cli