您的位置:首页 > 产品设计 > UI/UE

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

2016-09-19 22:04 465 查看
直接代码吧

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐