UICollectionView中的Alamofire + Swift [英] Alamofire + Swift in UICollectionView

查看:124
本文介绍了UICollectionView中的Alamofire + Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用Alamofire解析JSON数据.现在,我成功地从Web服务请求了数据.问题是我不确定如何将json(图像)数据嵌入/解析到UICollectionView

I had no idea how to parse JSON data using Alamofire. Right now I successfully request the data from web service. The problem is I'm not really sure how to embed/parse json (image) data into UICollectionView

import UIKit
import Alamofire

class PopularViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {

    var users: [AnyObject] = []

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return users.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserCell", forIndexPath: indexPath) as PopularCollectionViewCell

        Alamofire.request(.GET, "http://xxxxx/users.json").responseJSON { (_, _, data, _) in
            println(data)

        }
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

Json数据

{
    "users": [{
        "userId": 1,
        "profilePhoto": "https://graph.facebook.com/1301454197/picture?type=large"
    }]
}

推荐答案

我已经找到解决方案.

PopularViewController.swift

PopularViewController.swift

import UIKit
import Alamofire

class PopularViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {

    var users: [JSON] = []
    @IBOutlet var collectionView: UICollectionView!

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return users.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserCell", forIndexPath: indexPath) as PopularCollectionViewCell

        cell.user = self.users[indexPath.row]
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "xxxxx/users.json").responseJSON { (request, response, json, error) in

            if json != nil {
                var jsonObj = JSON(json!)
                if let data = jsonObj["users"].arrayValue as [JSON]?{
                    self.users = data
                    self.collectionView.reloadData()
                }
            }

        }
    }
}

PopularCollectionViewCell.swift

PopularCollectionViewCell.swift

import UIKit
import Haneke

class PopularCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var profilePicture:UIImageView!

    var user:JSON?{
        didSet{
            self.setupUser()
        }
    }

    func setupUser(){

        if let urlString = self.user?["profilePhoto"]{
            let url = NSURL(string: urlString.stringValue)
            self.profilePicture.hnk_setImageFromURL(url!)
        }

    }
}

这篇关于UICollectionView中的Alamofire + Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆