UITableView
的plain
style趴拧,section
的header
可以自動懸停珠月,footer
自動貼底,TableViewHeader
可以自動劃出屏幕。而UICollectionView
不能設(shè)置header
,UICollectionView
也沒有plain
模式蔫敲。
因?yàn)橛行﹫D片排版不太適合用UITableView
饲嗽,那么如何用UICollectionView
做出既包含header
,sectionHeader
又可以懸停的效果呢奈嘿?
方法一(適合items結(jié)構(gòu)簡單貌虾;也適合header上可以切換tab,tab展示數(shù)據(jù)比較類似,切換tab時只用刷新數(shù)據(jù)源就行)
- 思想:設(shè)置兩個
section
裙犹。第一組的section
的footer
來充當(dāng)整個UICollectionView
的header
效果劃出尽狠;然后設(shè)置第二組的header
,設(shè)置正常的items
叶圃,UICollectionView
的flowLayout
的sectionHeadersPinToVisibleBounds
屬性設(shè)置為true
則有懸停效果袄膏。
// Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView).
@available(iOS 9.0, *)
open var sectionHeadersPinToVisibleBounds: Bool
- demo(代碼很簡單,都貼出來了掺冠,方法非常取巧):
import UIKit
class ViewController: UIViewController {
private let itermSpace: CGFloat = 5
lazy private var flowLayout: UICollectionViewFlowLayout = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets.init(top: itermSpace, left: itermSpace, bottom: itermSpace, right: itermSpace)
flowLayout.minimumInteritemSpacing = itermSpace
flowLayout.minimumLineSpacing = itermSpace
flowLayout.sectionHeadersPinToVisibleBounds = true
return flowLayout
}()
lazy private var collectionView: UICollectionView = {
let colletionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
return colletionView
}()
override func viewDidLoad() {
super.viewDidLoad()
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200))
headerView.backgroundColor = UIColor.green
collectionView.register(TPPAddReviewCell.classForCoder(), forCellWithReuseIdentifier: "TPPAddReviewCell")
collectionView.register(TPPCollectionReusableViewHeader.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TPPCollectionReusableViewHeader")
collectionView.register(TPPCollectionReusableViewFooter.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "TPPCollectionReusableViewFooter")
collectionView.dataSource = self
collectionView.delegate = self
collectionView.frame = view.bounds
view.addSubview(collectionView)
}
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return 0
}
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TPPAddReviewCell", for: indexPath) as! TPPAddReviewCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 1 {
return CGSize(width: UIScreen.main.bounds.width, height: 50)
}
return CGSize.zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if section == 0 {
return CGSize(width: UIScreen.main.bounds.width, height: 200)
}
return CGSize.zero
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TPPCollectionReusableViewHeader", for: indexPath) as! TPPCollectionReusableViewHeader
return view
} else {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TPPCollectionReusableViewFooter", for: indexPath) as! TPPCollectionReusableViewFooter
return view
}
}
}
class TPPCollectionReusableViewHeader: UICollectionReusableView {
public override init(frame: CGRect) {
super.init(frame: frame)
configerUI()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configerUI() {
backgroundColor = UIColor.yellow
}
}
class TPPCollectionReusableViewFooter: UICollectionReusableView {
public override init(frame: CGRect) {
super.init(frame: frame)
configerUI()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configerUI() {
backgroundColor = UIColor.orange
}
}
class TPPAddReviewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
configureUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configureUI() {
contentView.backgroundColor = UIColor.red
}
}
-
效果:
方法二
思想:UITableView嵌套UITableView沉馆。在外層UITableView中的其中一個Cell中包裹多個VC,多個VC之間的結(jié)構(gòu)大不相同德崭,需要處理UITableView的手勢沖突問題斥黑,實(shí)現(xiàn)復(fù)雜,但是結(jié)構(gòu)清晰眉厨。類似這種心赶,切換tab,兩邊的結(jié)構(gòu)完全不同缺猛。這種方式比較麻煩缨叫,這里只提供一種思路,demo下次再說荔燎。