Swift UICollectionView 简单使用
2015-06-15 21:42
639 查看
最近要研究下排布的游戏关卡界面的实现,简单做了个UICollectionView的demo。
先看最后的效果:
下面来看实现的方法把,在Storyboard对应的ViewController中增加一个UICollectionView控件,然后再其中加入一个CollectionViewCell
在其中增加一个Label控件
注意,下面对这个Cell进行命名,命名成defaultCell, 这样我们UI层面的工作就结束了。
代码部分:
首先我们需要了解两个类,UICollectionViewDataSource和UICollectionViewDelegate
UICollectionViewDataSource负责提供提供View所需要的数据源
UICollectionViewDelegate负责处理View对应的各种事件
之后运行,你就可以看到效果啦。
先看最后的效果:
下面来看实现的方法把,在Storyboard对应的ViewController中增加一个UICollectionView控件,然后再其中加入一个CollectionViewCell
在其中增加一个Label控件
注意,下面对这个Cell进行命名,命名成defaultCell, 这样我们UI层面的工作就结束了。
代码部分:
首先我们需要了解两个类,UICollectionViewDataSource和UICollectionViewDelegate
UICollectionViewDataSource负责提供提供View所需要的数据源
UICollectionViewDelegate负责处理View对应的各种事件
class MyCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{ @IBOutlet weak var cv: UICollectionView! override func viewDidLoad() { super.viewDidLoad() cv.dataSource = self cv.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //实现UICollectionViewDataSource func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //返回记录数 return 100; } //实现UICollectionViewDataSource func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { //返回Cell内容,这里我们使用刚刚建立的defaultCell作为显示内容 var cell:MyColletionCell = cv.dequeueReusableCellWithReuseIdentifier("defaultCell", forIndexPath: indexPath) as! MyColletionCell cell.label.text = "\(indexPath.section):\(indexPath.row)" return cell; } //实现UICollectionViewDataSource func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { //某个Cell被选择的事件处理 } }
之后运行,你就可以看到效果啦。
相关文章推荐
- Swift学习笔记十三
- swift语言IOS8开发战记24 解析Json
- 第一个swift程序
- 窥探Swift之基本数据类型
- swift1 > start
- Swift EventKit的初学者指南--请求权限
- swift override --有一个递归问题未解决
- Awesome Swift
- 用Swift和Core Animatoin创建圆形图片加载动画
- Swift学习笔记(1)- 基础部分(上)
- playSwift第五章(函数和闭包)
- ios_swift开发资源整理
- swift Int max min
- swift generics 泛型
- swift extension Int
- swift willSet didSet
- Swift中的required修饰符
- swift enum Int
- swift get,set
- swift protocol