Skip to content

为 UICollectionView 添加 Header/FooterView 和 SectionView

License

Notifications You must be signed in to change notification settings

why19910522/WHYCollectionView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WHYCollectionView

Language License

为 UICollectionView 添加 Header/FooterView

使用方法

基本与 UICollectionView 的使用方法一致

添加 CollectionHeaderView 或者 CollectionFooterView 的方法与 UITableView 的方式一致

scrollDirection属性在设置时需要同时设置flowLayout里的scrollDirection, 否则会显示错误

collectionInset属性设置的是 WHYCollectionView 中 UICollectionView 的 contentInset , 并非 WHYCollectionView 自身的内边距

##示例代码

class ViewController: UIViewController {
    
    let cellId = "test"

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let flowLayout                     = UICollectionViewFlowLayout()
        flowLayout.itemSize                = CGSize(width: 100, height: 100)
        flowLayout.minimumInteritemSpacing = 10
        flowLayout.minimumLineSpacing      = 10
        flowLayout.scrollDirection         = .horizontal
        
        let collectionView = WHYCollectionView(with: view.bounds, layout: flowLayout)
        collectionView.backgroundColor = .white
        collectionView.collectionDataSource = self
        collectionView.collectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        collectionView.scrollDirection = .horizontal
        
        view.addSubview(collectionView)
        
        collectionView.register(cellClass:UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: cellId)
        
        let headerView = UIView(frame: CGRect(x: 110, y: 0, width: 60, height: 60))
        headerView.backgroundColor = .red
        
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
        footerView.backgroundColor = .green
        
        collectionView.collectionHeaderView = headerView
        collectionView.collectionFooterView = footerView
    }
}

extension ViewController: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
        cell.backgroundColor = .cyan
        
        return cell;
    }
}

About

为 UICollectionView 添加 Header/FooterView 和 SectionView

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages