zoukankan      html  css  js  c++  java
  • Swift控件UICollectionView使用学习总结

    UICollectionView —— Swift

    对 Swift开发UICollectionView的用法总结

     

    import UIKit
    
    class ThirdViewController: UIViewController,
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout {
        
        let item_identifier: String = "ThirdVCCollCell"
        let headerView_identifier: String = "ThirdVCCollHeaderView"
        
        let vcClassArr = ["UIScrollViewStudyVc","UITextFieldStudyVc","UILabelStudyVc"]
    
        var infoCollectionView:UICollectionView? = nil
        
        override func viewDidLoad() {
            super.viewDidLoad()
            print("vcClassArr:(vcClassArr)")
            view.addSubview(self.initInfoCollectionView())
            // Do any additional setup after loading the view.
        }
    
        func initInfoCollectionView() -> UICollectionView {
            
            let layout = UICollectionViewFlowLayout()
            layout.itemSize = CGSize(80,height: 60)                         //设置item尺寸
            layout.minimumLineSpacing = (UIScreen.main.bounds.width - 240) * 0.2  //上下间隔
            layout.minimumInteritemSpacing = (UIScreen.main.bounds.width - 240) * 0.1 //左右间隔
            layout.headerReferenceSize = CGSize(10,height:  30)                 //头部间隔
            layout.footerReferenceSize = CGSize(0,height:  0)               //底部间隔
            layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)                //section四周的缩进
            layout.scrollDirection = UICollectionViewScrollDirection.vertical     //滚动方向
        
            self.infoCollectionView  = UICollectionView.init(frame: view.bounds, collectionViewLayout: layout)
            self.infoCollectionView?.delegate = self
            self.infoCollectionView?.dataSource = self
            self.infoCollectionView?.backgroundColor = UIColor.white
            //注册cell(使用xib自定义的collectionViewCell)
            self.infoCollectionView?.register(UINib.init(nibName: "ThirdVCCollCell", bundle: nil), forCellWithReuseIdentifier: item_identifier)
            //注册headerView
            self.infoCollectionView?.register(ThirdVCCollHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier)
            return self.infoCollectionView!
        }
        
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 6
        }
        
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 11
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            
            let itemCell: ThirdVCCollCell = collectionView.dequeueReusableCell(withReuseIdentifier: item_identifier, for: indexPath) as! ThirdVCCollCell
            itemCell.backgroundColor = UIColor.red;
            itemCell.setTitleLabelText(titleText:"第(indexPath.section + 1)组")
            itemCell.setSubTitleLabelText(titleText:"第(indexPath.item + 1)个Item")
            return itemCell;
        }
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("当前点击第(indexPath.section + 1)组,第(indexPath.item + 1)个Item")
            if indexPath.section == 0{
                if indexPath.row < 3{
                    let vcClassName = self.vcClassArr[indexPath.section]
                    //字符串转 class
                    let vcClass = NSClassFromString("swift_study1." + vcClassName) as! NSObject.Type;//"包名.类名"
                    let vc:UIViewController = vcClass.init() as! UIViewController;
                    vc.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }
        }
        
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            
            return CGSize.init( self.view.frame.size.width, height: 30.0)
        }
        
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            
            if kind == UICollectionElementKindSectionHeader{
                
                let headerView:ThirdVCCollHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier, for: indexPath) as! ThirdVCCollHeaderView
                headerView.setTitleLabelText(title: "第(indexPath.section)组")
                return headerView
                
            }else if kind == UICollectionElementKindSectionFooter{
                
                
            }
            
            return UICollectionReusableView()
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    

     

     xib自定义collectionViewCell的方法:

     其中自定义的collectionViewCell的.xib文件的布局约束和object-c的方式相同如下图

    自定义collectionViewCell的.swift文件的代码:

    import UIKit
    
    class ThirdVCCollCell: UICollectionViewCell {
    
        @IBOutlet weak var title_label: UILabel!
        @IBOutlet weak var subTitle_label: UILabel!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
        
        func setTitleLabelText(titleText:String){
            if !titleText.isEmpty{
                self.title_label.text = titleText
            }
        }
        
        func setSubTitleLabelText(titleText:String){
            if !titleText.isEmpty{
                self.subTitle_label.text = titleText
            }
        }
    }
    

     效果如下图:

  • 相关阅读:
    paddlepaddle训练网络的基本流程二(进阶Paddle-detection框架)
    paddlepaddle训练网络的基本流程一(入门示例)
    redis常用操作命令
    mysql基础命令
    使用Gunicorn和nginx进行Flask服务部署
    如何测试(八)抖音 如何测试?
    全(十九)Jmeter 请求 之 遇到 cookie、token 处理方式(使用 正则表达式提取器 获取 然后在引用)
    全(十八)Jmeter 请求元件 之 JSON PATH 提取 响应结果
    全(十七)Jmeter 请求元件 之 正则表达式提取器 提取 响应结果、foreach循环控制器
    全(十六)Jmeter 请求 之 正则表达式
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/9310105.html
Copyright © 2011-2022 走看看