直接代码吧
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