Мои изображения не отображаются в моем UICollectionView. Я не уверен, в чем ошибки. Ниже мой код.Swift - изображение не отображается в моем 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: "[email protected]")
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 или что бы то ни было расширение. – Skywalker
Создан ли ваш MyCollectionViewCell с xib-файлом? Попробуйте создать imageView в awakeFromNib-методе, если вы создаете файл xib. В противном случае попробуйте создать imageView в initWithCoder: тоже – hiennv92