嘿,伙计们会喜欢我的第一个问题的帮助。我花了很长时间浏览论坛,但仍在努力理解我在哪里出错了。URL 字符串从 JSON 中解析得很好,但是它没有在我创建的 UIImageView 中显示为图像。代码编译正常,其他一切都在显示。我假设我必须将 String 类型的 URL 转换为 UIImageView 但是我不断收到错误。
这是 EventCell 类的片段。
class EventCell: UICollectionViewCell {
var event: Event? {
didSet {
if let eventName = event?.title {
eventTitle.text = eventName
}
eventSubtitle.text = event?.subtitle
eventTags.text = event?.tags
if let imageName = event?.imageURL
{
imageView.image = UIImage(named: imageName)
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let imageView: UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
return iv
}()
这是 Event 类本身的片段。
var id: NSNumber?
var title: String?
var imageURL: String?
var subtitle: String?
var tags: String?
var desc: String?
override init(){}
init(event: NSDictionary)
{
if let val = event["ID"] as? NSNumber
{
id = val;
}
if let val = event["Title"] as? String
{
title = val;
}
if let val = event["Subtitle"] as? String
{
subtitle = val;
}
if let val = event["Tags"] as? String
{
tags = val;
}
if let val = event["Description"] as? String
{
desc = val;
}
if let val = event["image"] as? String
{
imageURL = val;
}
}
任何帮助将非常感激!谢谢!