我试图编写应用程序,数据可以从Json显示并显示在collectionview中,但问题是只能显示文本而不能显示图片。有人可以帮忙,或给我一些建议吗?
var collection = [LabelClass]()
let kiaLoandURL = "http://xxxx.com/?json=get_recent_posts/json=1&count=10&custom_fields=PostThumb&include=title,content,thumbnail"
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CustomCollectionViewCell
cell.nameLabel.text = collection[indexPath.row].title
cell.nameImage.image = UIImage(named: collection[indexPath.row].image) // not working
return cell
}
func parseJsonData(data:NSData) -> [LabelClass] {
var info = [LabelClass]()
var error:NSError?
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary
if error != nil{
println(error?.localizedDescription)
}
let json = jsonResult?["posts"] as [AnyObject]
for jsonLoad in json {
let data = LabelClass()
data.title = jsonLoan["title"] as String
data.image = jsonLoan["thumbnail"] as String
info.append(data)
}
return info
}
答案 0 :(得分:0)
对于项目中包含的图片,您只能将UIImage
函数与named:
参数一起使用。
您应该尝试使用此处使用的方法: Loading/Downloading image from URL on Swift