【发布时间】:2015-10-21 07:19:47
【问题描述】:
我的图片没有显示在我的 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)!)!)
【问题讨论】:
-
如果我使用程序化方式可以吗?
-
尝试输入图片的扩展名,例如 blabla_iPad.png 或 blabla_iPad.jpg 或任何扩展名。
-
您的 MyCollectionViewCell 是用 xib 文件创建的吗?如果您使用 xib 文件创建,请尝试在 awakeFromNib 方法中创建 imageView。否则,尝试在 initWithCoder 中创建 imageView: too
-
没有 xib 文件。只是 Main.Storyboard。
-
你的细胞真的出现了吗?您是否尝试设置背景颜色?您的资产看起来如何?你为什么在名字里加上@2x?它也是资产的名称还是没有@2x?