前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift纯代码构建UICollectionView

Swift纯代码构建UICollectionView

作者头像
热心的程序员
发布2018-08-30 10:31:10
1.7K0
发布2018-08-30 10:31:10
举报
文章被收录于专栏:编程之路编程之路

先看下效果图,很简洁,没有任何样式。

效果图

接下来就是具体的实现。

1. 创建ViewController 命名为SHomeViewController。
2. 声明 UICollectionView。
代码语言:javascript
复制
var colltionView : UICollectionView?
3. 设置代理,这里Xcode会提示有错误,暂时不用管,实现UICollectionView的代理方法后这个错误自然就没有了。
代码语言:javascript
复制
class SHomeViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
4.实现UICollectionView的代理方法。
代码语言:javascript
复制
//返回多少个组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    //返回多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    //返回自定义的cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SHomeCell
        cell.titleLabel?.text = "wangjie"
       
        return cell
    }
5.自定义UICollectionViewCell为SHomeCell
代码语言:javascript
复制
//
//  SHomeCell.swift
//
//  Created by wangjie on 16/5/4.
//  Copyright © 2016年 wangjie. All rights reserved.
//

import UIKit

class SHomeCell: UICollectionViewCell {
    
    let width = UIScreen.mainScreen().bounds.size.width//获取屏幕宽
    var titleLabel:UILabel?//title
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initView(){
        titleLabel = UILabel(frame: CGRectMake(5, 5, (width-40)/2, 50))
        self .addSubview(titleLabel!)
    }
}
6.初始化UICollectionView并注册UICollectionViewCell。
代码语言:javascript
复制
        let layout = UICollectionViewFlowLayout()
        colltionView = UICollectionView(frame: CGRectMake(0, 0, width, height), collectionViewLayout: layout)
        //注册一个cell
        colltionView! .registerClass(SHomeCell.self, forCellWithReuseIdentifier:"cell")
        colltionView?.delegate = self;
        colltionView?.dataSource = self;
        colltionView?.backgroundColor = UIColor.whiteColor()
        //设置每一个cell的宽高
        layout.itemSize = CGSizeMake((width-30)/3, 30)
        self.view.addSubview(colltionView!)
7.设置数据源
代码语言:javascript
复制
    func getData(){
        dataArr.addObject("Tomcat")
        dataArr.addObject("Jetty")
        dataArr.addObject("Apache")
        dataArr.addObject("Jboss")
    }
8.SHomeViewController完整代码。
代码语言:javascript
复制
//
//  SHomeViewController.swift
//
//  Created by wangjie on 16/5/4.
//  Copyright © 2016年 wangjie. All rights reserved.
//

import UIKit

class SHomeViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    var colltionView : UICollectionView?
    var dataArr = NSMutableArray()//数据源
    let width = UIScreen.mainScreen().bounds.size.width//获取屏幕宽
    let height = UIScreen.mainScreen().bounds.size.height//获取屏幕高

    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    func initView(){
        let layout = UICollectionViewFlowLayout()
        colltionView = UICollectionView(frame: CGRectMake(0, 0, width, height), collectionViewLayout: layout)
        //注册一个cell
        colltionView! .registerClass(SHomeCell.self, forCellWithReuseIdentifier:"cell")
        colltionView?.delegate = self;
        colltionView?.dataSource = self;
        colltionView?.backgroundColor = UIColor.whiteColor()
        //设置每一个cell的宽高
        layout.itemSize = CGSizeMake((width-30)/3, 30)
        self.view.addSubview(colltionView!)
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    //返回多少个组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    //返回多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    //返回自定义的cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SHomeCell
        var title = String()
        title = dataArr[indexPath.row] as! String
        cell.titleLabel?.text = title
       
        return cell
    }
    
    func getData(){
        dataArr.addObject("Tomcat")
        dataArr.addObject("Jetty")
        dataArr.addObject("Apache")
        dataArr.addObject("Jboss")
    } 
}
9.运行即可看见效果。从这个例子中我们发现Swift语言还是很简洁的。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 创建ViewController 命名为SHomeViewController。
  • 2. 声明 UICollectionView。
  • 3. 设置代理,这里Xcode会提示有错误,暂时不用管,实现UICollectionView的代理方法后这个错误自然就没有了。
  • 4.实现UICollectionView的代理方法。
  • 5.自定义UICollectionViewCell为SHomeCell
  • 6.初始化UICollectionView并注册UICollectionViewCell。
  • 7.设置数据源
  • 8.SHomeViewController完整代码。
  • 9.运行即可看见效果。从这个例子中我们发现Swift语言还是很简洁的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档