如何在collectiveview(Json)中显示图片?

时间:2015-04-11 15:11:17

标签: json swift uicollectionview

我试图编写应用程序,数据可以从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
   }

1 个答案:

答案 0 :(得分:0)

对于项目中包含的图片,您只能将UIImage函数与named:参数一起使用。

您应该尝试使用此处使用的方法: Loading/Downloading image from URL on Swift