學(xué)習(xí)文章
一 初步UICollectionView
使用UICollectionView的流程:
設(shè)定一個(gè)UICollectionViewFlowLayout
使用這個(gè)設(shè)定的UICollectionViewFlowLayout來(lái)初始化UICollectionView
設(shè)置代理對(duì)象
繼承UICollectionViewCell設(shè)定重用的cell
效果
源碼:
LargeUICollectionViewFlowLayout
import UIKit
class LargeUICollectionViewFlowLayout: UICollectionViewFlowLayout {
override init() {
super.init()
// 單元格尺寸
self.itemSize = CGSize(width: 70, height: 70)
// section 內(nèi)間距
self.sectionInset = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)
// 橫排單元格最小間距
self.minimumInteritemSpacing = 40
// 單元格最小行間距
self.minimumLineSpacing = 5
// CollectionView滾動(dòng)方向
self.scrollDirection = .Vertical
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ShowCollectionViewCell
import UIKit
let identifier = "Identifier"
class ShowCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.redColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ViewController
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// 初始化UICollectionView并指定一個(gè)UICollectionViewFlowLayout
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: LargeUICollectionViewFlowLayout())
collectionView?.registerClass(ShowCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: identifier)
collectionView?.dataSource = self
collectionView?.delegate = self
view.addSubview(collectionView!)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
return cell
}
}
重要的參數(shù)
二 實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求
效果
源碼
修改ShowCollectionViewCell
import UIKit
let identifier = "Identifier"
class ShowCollectionViewCell: UICollectionViewCell {
var showImageView: UIImageView?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.whiteColor()
var rect: CGRect = self.bounds
rect.origin.x += 3
rect.origin.y += 3
rect.size.width -= 6
rect.size.height -= 6
showImageView = UIImageView(frame:rect)
self.addSubview(showImageView!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
修改ViewController
import UIKit
let sourceUrl = "http://www.duitang.com/album/1733789/masn/p/0/100/"
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView?
var dataArray: [AnyObject]?
override func viewDidLoad() {
super.viewDidLoad()
// 初始化數(shù)據(jù)源
dataArray = [AnyObject]()
// 初始化UICollectionView并指定一個(gè)UICollectionViewFlowLayout
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: LargeUICollectionViewFlowLayout())
collectionView?.registerClass(ShowCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: identifier)
collectionView?.dataSource = self
collectionView?.delegate = self
view.addSubview(collectionView!)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
// 獲取json數(shù)據(jù)
let data = NSData(contentsOfURL: NSURL(string: sourceUrl)!)
// 轉(zhuǎn)換數(shù)據(jù)
if let dataDic = try? NSJSONSerialization.JSONObjectWithData(data!, options: [.MutableContainers, .MutableLeaves]) as! [String : AnyObject]{
let array = dataDic["data"]!["blogs"] as! [AnyObject]
for value in array {
let temp = value as! [String : AnyObject]
print(temp["isrc"])
self.dataArray?.append(temp["isrc"]!)
}
}
// 主線程更新
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.collectionView?.reloadData()
})
}
}
// MARK: UICollectionViewDataSource
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (self.dataArray?.count)!
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ShowCollectionViewCell
cell.showImageView?.sd_setImageWithURL(NSURL(string: self.dataArray![indexPath.row] as! String))
return cell
}
}
三 實(shí)時(shí)更換layout
效果
源碼
import UIKit
class AnotherCollectionViewFlowLayout: UICollectionViewFlowLayout {
override init() {
super.init()
// 單元格尺寸
self.itemSize = CGSize(width: 150, height: 200)
// section 內(nèi)間距
self.sectionInset = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)
// 橫排單元格最小間距
self.minimumInteritemSpacing = 40
// 單元格最小行間距
self.minimumLineSpacing = 5
// CollectionView滾動(dòng)方向
self.scrollDirection = .Vertical
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}