• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift:我的第四个Demo

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

这次Demo是关于UICollectionView的

//
//  ViewController.swift
//  UIKitPrograming
//

import UIKit

class ViewController: UIViewController {

    // 定义一些不变量
    let ScreenWidth = UIScreen.main.bounds.width // 屏幕宽度的一半
    let cellIdentifier = "cell"
    let headerIdentifier = "header"
    let footerIdentifier = "footer"
    let headerKind = UICollectionView.elementKindSectionHeader
    let footerKind = UICollectionView.elementKindSectionFooter
    
    // 返回一个随机的颜色
    func randomColor() -> UIColor {
        return UIColor(displayP3Red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 先设置布局,这是collectionView必须做的
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.itemSize = CGSize(width: ScreenWidth / 7 - layout.minimumInteritemSpacing * 2, height: ScreenWidth / 7 - layout.minimumInteritemSpacing * 2) // 设置方块的长和宽
        layout.scrollDirection = .vertical // 垂直滚动
        layout.sectionInset = .init(top: 5, left: 5, bottom: 5, right: 5) // 设置每个方块的四周空隙
        layout.headerReferenceSize = CGSize(width: ScreenWidth, height: 80) // 设置header大小
        layout.footerReferenceSize = CGSize(width: ScreenWidth, height: 80)
        
        // 第二步是创建一个UICollectionView的实例
        let colletionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
        colletionView.backgroundColor = UIColor.white
        // 设置代理
        colletionView.delegate = self
        colletionView.dataSource = self
        
        // 注册方块
        colletionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        colletionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: headerKind, withReuseIdentifier: headerIdentifier)
        colletionView.register(CollectionFooterView.self, forSupplementaryViewOfKind: footerKind, withReuseIdentifier: footerIdentifier)
        
        // 别忘了最后要添加实例
        view.addSubview(colletionView)
    }
}

// 下面要实现委托的内容
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    // 一个section有多少个方块
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 40
    }
    
    // 几个section
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    // 返回cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // cell一定要用dequeueReusableCell的方法去取
        let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
        cell.backgroundColor = randomColor() // 设置cell的颜色随机
        return cell
    }

    // 点击方块,打印他的编号
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    // 这个函数是针对header和footer来做的,返回值是UICollectionReusableView类型,这个类型继承自UIView,因此可以理解成一个UIView
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == headerKind {
            // CollectionHeaderView就是UICollectionReusableView类型的
            let headView: CollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: headerKind, withReuseIdentifier: headerIdentifier, for: indexPath) as! CollectionHeaderView
            headView.title.text = "This is HeaderView"
            headView.backgroundColor = UIColor.gray
            headView.setUpView()
            return headView
        } else {
            let footerView: CollectionFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: footerKind, withReuseIdentifier: footerIdentifier, for: indexPath) as! CollectionFooterView
            footerView.title.text = "This is FooterView"
            footerView.backgroundColor = UIColor.gray
            footerView.setUpView()
            return footerView
        }
    }
}
//
//  CollectionHeaderView.swift
//  UIKitPrograming
//

import UIKit
import SnapKit

class CollectionHeaderView: UICollectionReusableView {
    lazy var title: UILabel = {
        let title: UILabel = UILabel()
        title.font = UIFont.boldSystemFont(ofSize: 14)
        title.textColor = UIColor.black
        title.textAlignment = .center
        return title
    }()
    
    
    func setUpView() {
        addSubview(title)
        title.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
    }
}
//
//  CollectionFooterView.swift
//  UIKitPrograming
//

import UIKit

class CollectionFooterView: UICollectionReusableView {
    lazy var title: UILabel = {
        let title: UILabel = UILabel()
        title.font = UIFont.boldSystemFont(ofSize: 14)
        title.textColor = UIColor.black
        title.textAlignment = .center
        return title
    }()
    
    
    func setUpView() {
        addSubview(title)
        title.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swiftoc混编-oc导入Swift协议发布时间:2022-07-13
下一篇:
swift调用支付宝发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap