佈局推薦界面
實現效果
- 今天內容完成效果
推薦界面分析
- 上層有無限輪播器(之後再完成)
- 滾動的UIScrollView或者UICollectionView(之後再完成)
- 下面是UICollectionView,並且有HeaderView
- UICollectionView的Cell大小基本一致,但是在顏值處Cell的高度有變化。
- 該UICollectionView一共12組,第一組8個Item,其他組均4個Item
佈局UICollectionView
- 在控制器的View中添加UICollectionView
- 懶加載UICollectionView
- 將UICollectionView添加到控制器的View中
- 特殊屬性設置:
- headerReferenceSize : 設置頭部的Size
- sectionInset :設置每組的內邊距
private let kItemMargin : CGFloat = 10 private let kItemW : CGFloat = (kScreenW - 3 * kItemMargin) / 2 private let kNormalItemH : CGFloat = kItemW * 3 / 4 private let kPrettyItemH : CGFloat = kItemW * 4 / 3 private let kHeaderViewH : CGFloat = 50
// MARK:- 懶加載屬性 private lazy var collectionView : UICollectionView = {[unowned self] in // 1.創建佈局 let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width: kItemW, height: kNormalItemH) layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = kItemMargin layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH) layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
// 2.創建UICollectionView let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout) collectionView.dataSource = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: kNormalCellID)
return collectionView }()
// MARK:- 遵守UICollectionView的數據源協議 extension RecommendViewController : UICollectionViewDataSource { func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 12 }
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if section == 0 { return 8 }
return 4 }
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kNormalCellID, forIndexPath: indexPath)
cell.backgroundColor = UIColor.redColor()
return cell } }
- 展示效果如下:
- 實現效果
- 實現思路
- 自定義UICollectionReusableView, 用於作爲HeaderView
- 可以通過Xib直接描述
- 在註冊Cell時,使用Nib進行註冊
- 代碼如下:
collectionView.registerNib(UINib(nibName: "RecommendHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
- 效果如下:
佈局普通的UICollectionViewCell
- 實現效果
- 實現思路
- 自定義UICollectionViewCell,用於作爲普通的Cell
- 可以通過Xib直接描述
- 在註冊Cell時,使用Nib進行註冊
- 代碼如下:
collectionView.registerNib(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellID)
- 效果如下:
佈局顏色的UICollectionViewCell
- 實現效果
- 實現思路
- 自定義UICollectionViewCell,用於作爲顏值的Cell
- 可以通過Xib直接描述
- 在註冊Cell時,同時註冊顏值的Cell
- 代碼如下
collectionView.registerNib(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : CollectionBaseCell!
if indexPath.section == 1 { cell = collectionView.dequeueReusableCellWithReuseIdentifier(kPrettyCellID, forIndexPath: indexPath) as! CollectionBaseCell } else { cell = collectionView.dequeueReusableCellWithReuseIdentifier(kNormalCellID, forIndexPath: indexPath) as! CollectionBaseCell }
return cell }
- 另外,因爲NormalCell&PrettyCell高度不同, 因此需要設置代理,並且實現代理方法
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { if indexPath.section == 1 { return CGSize(width: kItemW, height: kPrettyItemH) } else { return CGSize(width: kItemW, height: kNormalItemH) } }
- 效果如下:
|