开发者社区> 问答> 正文

如何在UICollectionView中添加Header?

我建了一个collectionview并且无法添加它的标题,因为我阅读了API,其中说要添加一个UICollectionViewCompositionalLayout.

但它只是支持ios 13或更高版本。

有什么功能可以替代吗?

展开
收起
游客5akardh5cojhg 2019-12-09 00:13:49 798 0
1 条回答
写回答
取消 提交回答
  • 这里我的代码没有故事板

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let flowLayout = UICollectionViewFlowLayout()
            flowLayout.itemSize = CGSize(width: 100, height: 100)
            flowLayout.scrollDirection = .vertical
            let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), collectionViewLayout: flowLayout)
            collection.backgroundColor = .white
            collection.delegate = self
            collection.dataSource = self
    
            self.view.addSubview(collection)
    
            collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "one")
            collection.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
            collection.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")
        }
    
        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 {
            return CGSize(width: self.view.frame.size.width, height: 50)
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
            return CGSize(width: self.view.frame.size.width, height: 50)
        }
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 2
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            switch kind {
            case UICollectionElementKindSectionHeader:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)
    
                headerView.backgroundColor = UIColor.blue
                return headerView
    
            case UICollectionElementKindSectionFooter:
                let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)
                footerView.backgroundColor = UIColor.green
                return footerView
    
            default:
    
                assert(false, "Unexpected element kind")
            }
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "one", for: indexPath)
            let title = UILabel(frame: CGRect(x: 0, y: 0, width: cell.bounds.size.width, height: 40))
            title.text = String(indexPath.row)
            cell.contentView.backgroundColor = .gray
            cell.contentView.addSubview(title)
            return cell
        }
    }
    
    2019-12-09 00:14:27
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载