• 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方法

  • 相关阅读:
    feedparser win7 python 安装
    Binary Tree Inorder Traversal
    SDUT2013级測试赛_D
    Eclipse中在android项目中出现新建一个Activity后,出现整个project的报错以及包导入以后无法执行等等情况分析。
    代码高亮 highlightjs 使用文档
    android用jsonReader来解析json
    策略模式Strategy——回家乘什么车?
    【转】Android UI系列-----时间、日期、Toasts和进度条Dialog
    【转】24. android dialog ——ProgressDialog 进度条对话框详解
    【转】我的Android笔记(十)—— ProgressDialog的简单应用,等待提示
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6510434.html
Copyright © 2020-2023  润新知