- collectionView是iOS中一個(gè)非常重要的空間器贩,但是使用的頻率可能不高颅悉,方法相對(duì)來(lái)說和tableview有所不同衔统,記錄一下提针,防止遺忘
- 1.創(chuàng)建UICollectionViewLayout
- 2.創(chuàng)建UICollectionView
- 3.設(shè)置數(shù)據(jù)源方法
- 4.設(shè)置代理方法
- 5.注冊(cè)cell
1.創(chuàng)建UICollectionViewLayout
- 過去一直認(rèn)為collectionView比較難使用藕甩,尤其是
UICollectionViewLayout
施敢,不知道該怎么設(shè)置,后來(lái)突然想到了狭莱,其實(shí)UICollectionViewLayout
和tableview的UITableViewStyle.Plain
是一樣的僵娃,就是一個(gè)必設(shè)的屬性。只不過**UICollectionViewLayout **是具體的item的大小腋妙,樣式默怨,間距等等屬性,我們要用具體的flow
實(shí)例化對(duì)象
private let flowLayout : UICollectionViewFlowLayout = {
let flt = UICollectionViewFlowLayout()
flt.minimumLineSpacing = 0
flt.minimumInteritemSpacing = 0
flt.scrollDirection = UICollectionViewScrollDirection.Horizontal
flt.itemSize = UIScreen.mainScreen().bounds.size
return flt
}()
2.創(chuàng)建UICollectionView
- 因?yàn)槲艺J(rèn)為骤素,collectionView的生成匙睹,他要變成什么樣子的愚屁,他自己最清楚,所以就講flowLayout的的創(chuàng)建全部放到collectionViewController的內(nèi)部
init(){
super.init(collectionViewLayout:flowLayout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
**init 之所以沒有去寫override,是因?yàn)閏ollectionVC默認(rèn)的方法是init(collectionViewLayout layout: UICollectionViewLayout) **
設(shè)置一下collectionView的基本屬性痕檬,可以分頁(yè)霎槐,和橫向滑動(dòng),豎直方法滑動(dòng)
self.collectionView!.bounces = false
collectionView?.pagingEnabled = true
collectionView?.showsHorizontalScrollIndicator = false
3.設(shè)置數(shù)據(jù)源方法
如果vc繼承于UICollectionViewController的話梦谜,extension方法這樣寫
extension WXNewFeatureController{
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! WXNewFeatureCell
cell.imageIndex = indexPath.item
return cell
}
}
4.設(shè)置代理方法
代理方法可以寫丘跌,也可以不寫~~
5.注冊(cè)cell
如果懶得自定義就用系統(tǒng)的
//聲明一個(gè)常量
private let reuseIdentifier = "Cell"
//去注冊(cè)
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(WXNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
//調(diào)用
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
return cell
}
6.自定義cell
因?yàn)橐恢绷?xí)慣封裝,而且可以各種自定義內(nèi)部控件唁桩,所以就去封裝一下闭树,這個(gè)cell就是在這個(gè)collectionViewController中使用,所以我么就將這個(gè)cell的創(chuàng)建放到這個(gè)文件中荒澡,而且創(chuàng)建可以使用private修飾报辱,方法也可以用它修飾,在這個(gè)文件中仰猖,Private修飾的都可以隨意調(diào)用捏肢,哪怕不是同一個(gè)類都行
private class WXNewFeatureCell:UICollectionViewCell{
//對(duì)外開放一個(gè)index屬性
var imageIndex : NSInteger? {
didSet{
imageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
lab.text = "new_feature_\(imageIndex! + 1)"
//判斷如果是第4個(gè)頁(yè)面,我們就去添加一個(gè)按鈕
if imageIndex! + 1 == 4 {
confirmBtn.hidden = false
contentView.addSubview(confirmBtn)
}
}
}
//懶加載數(shù)據(jù)
private lazy var imageView : UIImageView = {
return UIImageView()
}()
private lazy var lab : UILabel = UILabel()
private lazy var confirmBtn:UIButton = {
let btn = UIButton()
btn.hidden = true
btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
return btn
}()
//MARK:-自定義一個(gè)collctionView
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//創(chuàng)建子控件
private func setupUI(){
contentView.addSubview(imageView)
contentView.addSubview(lab)
//創(chuàng)建完子控件饥侵,必須調(diào)用這個(gè)方法鸵赫,否則不去調(diào)用布局方法
setNeedsUpdateConstraints()
}
//初始化的時(shí)候,如果不去調(diào)用的話躏升,不走這個(gè)方法辩棒,很惡心,所以初始化完畢一定要去調(diào)用一下
override func updateConstraints() {
super.updateConstraints()
imageView.autoPinEdgesToSuperviewEdges()
lab.autoPinEdgesToSuperviewEdges()
if imageIndex == 3 {
let size = confirmBtn.currentBackgroundImage?.size
confirmBtn.autoSetDimensionsToSize(size!)
confirmBtn.autoAlignAxisToSuperviewAxis(ALAxis.Vertical)
confirmBtn.autoPinEdgeToSuperviewEdge(ALEdge.Bottom, withInset: 100)
}
}
}
7.flowLayout可以的自定義
我太懶膨疏,就不寫了一睁,可以在這個(gè)文件中寫,沒關(guān)系~