ios - 图像未显示在我的 Swift 中的 UICollectionView 中

标签 ios swift nsdata nsurl

我的图像没有显示在我的 UICollectionView 中。我不确定是什么错误。下面是我的代码。

MyCollectionView.swift

/** -- **/

    overide func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCollectionViewCell", forIndexPath: indexPath) as! MyCollectionViewCell
            let event = events[indexPath.row]

            cell.imageView?.image = UIImage(named: "blabla_iPad@2x")

            return cell

        }

/** -- **/

MyCollectionViewCell.swift

import UIKit

class MyCollectionViewCell: UICollectionViewCell {
    var imageView: UIImageView!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        imageView = UIImageView(frame: CGRect(x: 0, y: 16, width: frame.size.width, height: frame.size.height))
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        contentView.addSubview(imageView)
    }
}

即使我正在使用 localPath,它仍然无法正常工作。

/*
    /Users/MNurdin/Library/Developer/CoreSimulator/Devices/B30917DD-1C57-430F-9ACF-6526B226CF15/data/Containers/Data/Application/35ACB17A-4C25-4A49-BE3D-04AA51F36CC0/tmp/jreGfApABajWPCnkfPBh.jpg
*/

cell.imageView?.image = UIImage(data: NSData(contentsOfURL: NSURL(fileURLWithPath:localPath)!)!)

最佳答案

 var imageView: UIImageView!

这应该是一个 IBOutlet。

@IBOutlet weak var imageView: UIImageView!

并将其连接到您的 Storyboard。

关于ios - 图像未显示在我的 Swift 中的 UICollectionView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253196/

相关文章:

ios - UIButton 在 NavigationController 中没有响应

Swift 3 将数据转换为 AVAsset 或 PHAsset

swift - 如何在 Swift 中覆盖 setter

ios - 编码/解码 base64 解码数据 = nil

objective-c - 多线程环境下的 NSData 与 CCCrypt

ios - 如何制作褪色的导航栏

iOS-如何分解网页内容并根据需要显示它

arrays - 在两个对象数组之间过滤,避免嵌套 for 循环

ios - 如何在 iOS swift 中检索缓存文件?

ios - 在 iOS 7 中更改 UITableViewCellStyleValue2 textLabel 的整体 tintColor 和字体大小