Swift纯代码构建UICollectionView

作者: JeenWang | 来源:发表于2016-05-04 16:18 被阅读3519次

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


效果图

接下来就是具体的实现。

1. 创建ViewController 命名为SHomeViewController。
2. 声明 UICollectionView。
var colltionView : UICollectionView?

3. 设置代理,这里Xcode会提示有错误,暂时不用管,实现UICollectionView的代理方法后这个错误自然就没有了。
class SHomeViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
4.实现UICollectionView的代理方法。
//返回多少个组
    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
//
//  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。
        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.设置数据源
    func getData(){
        dataArr.addObject("Tomcat")
        dataArr.addObject("Jetty")
        dataArr.addObject("Apache")
        dataArr.addObject("Jboss")
    }
8.SHomeViewController完整代码。
//
//  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语言还是很简洁的。

相关文章

网友评论

  • bb9c4afd9fae:self .addSubview(titleLabel!) 这句self后面是啥?
    Cows:这没什么疑问的啊
    彧哥哥:在这个页面上添加入这个控件

本文标题:Swift纯代码构建UICollectionView

本文链接:https://www.haomeiwen.com/subject/usgrrttx.html