UICollectionViewCell
来源:互联网 发布:久坐肚子大 知乎 编辑:程序博客网 时间:2024/05/18 12:33
UICollectionViewDataSource
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
UICollectionViewCell
UICollectionViewLayout
1 UICollectionViewCell
- Cell中的View
- 管理Cell的状态
2 自定义UICollectionViewCell
- 创建xib
- 配置xib
- 类YJCollectionViewCell
3 实战演练
1 UICollectionViewCell
UICollectionViewCell就是UICollectionView显示的一个一个元素,你可以通过它定制各种精美的界面。原始样式很简单,就是常用的View。多数情况在开发中我们会自定义各种各样的通用Cell供其他人使用,这篇博文也会介绍自定义Cell。
1.1 Cell中的View
/// 自定义控件的父Viewvar contentView: UIView { get }/// 默认背景var backgroundView: UIView?/// 选中时的背景var selectedBackgroundView: UIView?
1.2 管理Cell的状态
/// 是否选中var selected: Bool/// 是否高亮var highlighted: Bool
2 自定义UICollectionViewCell
我们知道UICollectionViewCell是UICollectionReusableView的子类,而UICollectionReusableView是UICollectionView的Header和Footer。也就意味着自定义UICollectionViewCell的方式也同样适用于自定义Header和Footer。
多数情况下,我们开发过程中自定义UICollectionViewCell,不是在主VC界面直接创建的,而是通过xib或者纯代码创建。这样有利于公用,以及维护。
这里我讲解通过xib自定义UICollectionViewCell,这也是我最喜欢的方式。
2.1 创建xib
创建类YJCollectionViewCell继承UICollectionViewCell,同时勾选”Also create XIB file”。这样同时创建类和xib,并且二者已经关联上了,无须我们做其他配置。
2.2 配置xib
在YJCollectionViewCell.xib上UILable控件,如下所示。
2.3 类YJCollectionViewCell
类YJCollectionViewCell的源码如下所示。
//// YJCollectionViewCell.swift// UI//// CSDN:http://blog.csdn.net/y550918116j// GitHub:https://github.com/937447974/Blog//// Created by yangjun on 15/12/12.// Copyright © 2015年 阳君. All rights reserved.//import UIKit/// 自定义UICollectionViewCellclass YJCollectionViewCell: UICollectionViewCell { /// 显示内容 @IBOutlet weak var textLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code }}
这样就定制了一个通用的UICollectionViewCell。
3 实战演练
为了简单点演示效果,这里我使用YJCollectionViewCellVC,并且继承UICollectionViewController。
//// YJCollectionViewCellVC.swift// UI//// CSDN:http://blog.csdn.net/y550918116j// GitHub:https://github.com/937447974/Blog//// Created by yangjun on 15/12/19.// Copyright © 2015年 阳君. All rights reserved.//import UIKit/// 自定义UICollectionViewCellclass YJCollectionViewCellVC: UICollectionViewController { override func viewDidLoad() { super.viewDidLoad() let nib = UINib(nibName: "YJCollectionViewCell", bundle: nil) self.collectionView?.registerNib(nib, forCellWithReuseIdentifier: "customCell") } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 100 } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("customCell", forIndexPath: indexPath) as! YJCollectionViewCell cell.backgroundColor = UIColor.grayColor() cell.textLabel.text = "\(indexPath.item)" return cell }}
这里的核心是要使用UICollectionViewfunc registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
注册xib。这样有利于UICollectionViewCell的复用。
运行项目,能看到如下效果图:
这样就自定义了UICollectionViewCell。相信看完后,你也能自定义你所需要的UICollectionViewCell。
其他
源代码
Swift
参考资料
UICollectionView Class Reference
UICollectionViewDataSource Protocol Reference
UICollectionReusableView Class Reference
UICollectionViewCell Class Reference
文档修改记录
版权所有
CSDN:http://blog.csdn.net/y550918116j
GitHub:https://github.com/937447974/Blog
- UICollectionViewCell
- UICollectionViewCell
- UICollectionViewCell 布局
- UICollectionViewCell复用
- 移动UICollectionViewCell
- UICollectionviewCell自适应label长度
- UICollectionViewCell的使用
- 设置UICollectionViewcell无间隙
- 关于UICollectionViewCell的问题
- UICollectionView 及 UICollectionViewCell 复用
- iOS 自定义UICollectionViewCell
- UICollectionViewCell 长按重排
- UICollectionViewCell的indexPath问题
- 设置UICollectionViewCell选中颜色
- Swift之自定义UICollectionViewCell
- 点击UICollectionViewCell上的UIButton如何获取当前的UICollectionViewCell?
- UICollectionViewCell上显示编辑菜单
- IOS中UICollectionViewCell的问题
- SSH工具无法连接VMware上的linux虚拟机解决办法
- iOS中GCD的基本使用
- UI01_UILabel
- 2-5-归并链式存储的单链表-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- Linux下怎么输入重音符号
- UICollectionViewCell
- android service 详解
- UFLDL教程答案(7):Exercise:Learning color features with Sparse Autoencoders
- PAT-统计个位数字(基础函数题)
- Shell
- poj 2253 Frogger【最短路】
- 基于用户的协同过滤算法(Java实现或R语言实现
- Android学习之多线程编程(handler篇)
- AddForce和velocity有什么区别