zoukankan      html  css  js  c++  java
  • swift学习

    swift CollectionView学习

    效果图:

    源码:
    ContModel.swift

    import UIKit
    
    class ContModel: NSObject {
    
        var title:String?
        var image:String?
        
        func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        }
    
    }
    

    ContViewCell.swift
    cell截图

    import UIKit
    
    class ContViewCell: UICollectionViewCell {
    
        @IBOutlet weak var img: UIImageView!
        @IBOutlet weak var titleLb: UILabel!
        
        var model:ContModel?{
            //重写set方法
            didSet{
                img.image = UIImage(named: model!.image! )
                titleLb.text = model?.title
            }
        }
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            self.layer.cornerRadius = 10
            self.clipsToBounds = true
        }
        
    }
    

    ViewController.swift

    import UIKit
    
    class ViewController: UIViewController {
     
        var collectionView:UICollectionView?
        var dataArr:NSMutableArray?
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            prepareData()
            setupCollectionView()
        }
        
        func prepareData() {
            dataArr = NSMutableArray()
            for i in 0..<7
            {
                let model = ContModel()
                model.image = "image(i)"
                model.title = "xxx(i)"
                dataArr?.add(model)
            }
        }
    
        func setupCollectionView() {
            let layout = UICollectionViewFlowLayout();
            layout.scrollDirection = .horizontal
            layout.itemSize = CGSize( 300, height: 400)
    //        layout.minimumLineSpacing = 5.0
            layout.minimumInteritemSpacing = 10.0
            let col = UICollectionView(frame:CGRect(x: 0, y: 100,  375, height: 400), collectionViewLayout: layout)
            col.dataSource = self
            col.backgroundColor = .clear;
            col.register(UINib.init(nibName: "ContViewCell", bundle: nil), forCellWithReuseIdentifier: "ContViewCell")
            collectionView = col
            view.addSubview(col)
        }
        
    }
    
    extension ViewController:UICollectionViewDataSource
    {
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
        
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return (dataArr?.count)!
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContViewCell", for: indexPath) as! ContViewCell
            let model = dataArr?[indexPath.row]
            cell.model = model as! ContModel?
            return cell
        }
        
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize( 200, height: 200)
        }
        
        
    }
    

    collectionView的方法和objc中的使用方法类似
    这几有几个重点需要注意
    1.使用UIVisualEffectView(iOS8以后才有的View)给view添加Effective效果
    2.怎么自定义uicollectionViewCell
    3.怎么创建自定义model,并重写set方法

  • 相关阅读:
    Java上传图片
    git下拉项目失败,报错 RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
    关于Java项目启动后浏览器访问本机磁盘里的图片报错Not allowed to load local resource
    window安装Redis和springboot整合Redis进行简单使用
    Java强弱密码校验,必须包含大小写字母、数字、特殊符号
    工作记录:C# ashx生成验证码图片及校验
    springboot配置逆向工程
    mysql学习总结(二)
    ISM模型:用python实现可达矩阵求解和层级划分
    mysql学习总结(一)
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6510434.html
Copyright © 2011-2022 走看看