先看下效果图,很简洁,没有任何样式。
效果图
接下来就是具体的实现。
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")
}
}
网友评论