IOS学习-使用Alamofire和UICollectionView完成列表展示

直接代码吧


import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var cv: UICollectionView!

    // 列表的数据,数组都是JSON类型的,这个是SwiftJSON的特点
    var d = Array<JSON>()

    override func viewDidLoad() {
        super.viewDidLoad()

        let p = ["pageIndex" : 1,
                 "pageSize" : 10,
                 "value" : ""]
        // 获取列表的数据
        Alamofire.request(.POST, "http://api.yourdomain.cn/api/Iqiyi/GetAlbumList", parameters: p).responseJSON() {
            res in
            if let data = res.result.value {
                var json = JSON(data)
                self.d = json["data"]["category"].arrayValue
                // 刷新列表
                self.cv.reloadData()
            }
        }
        // 设置数据源和委托
        self.cv.dataSource = self
        self.cv.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //列表的数量
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.d.count
    }

    // 构建cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = cv.dequeueReusableCellWithReuseIdentifier("defaultCell", forIndexPath: indexPath)
        let label = cell.viewWithTag(10001) as! UILabel
        label.text = self.d[indexPath.item].string!
        return cell
    }

    // cell选中
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("\(self.d[indexPath.item])")
    }
}

再简单的东西也要自己试一下

相关文章:
1. http://www.hangge.com/blog/cache/detail_968.html
2. http://www.jianshu.com/p/4acbf9632a5e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值