Swift UICollectionView 使用
来源:互联网 发布:织梦cms登录密码忘记 编辑:程序博客网 时间:2024/06/03 22:41
1.Controller 中添加 UICollectionView 控件
import Foundationimport UIKitclass MainPageViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { var collectionView:UICollectionView?; let CELL_ID = "cell_id"; let HEAD_ID = "head_id"; override func viewDidLoad() { super.viewDidLoad(); self.view.backgroundColor = UIColor.blueColor(); self.title = "首页"; createCollectionView(); } func createCollectionView() { let flowLayout = UICollectionViewFlowLayout(); collectionView = UICollectionView(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),collectionViewLayout:flowLayout); collectionView!.backgroundColor = UIColor.orangeColor(); collectionView!.delegate = self; collectionView!.dataSource = self; collectionView!.registerClass(MainPageSectionZeroCell.self, forCellWithReuseIdentifier: CELL_ID); collectionView!.registerClass(MainPageSectionZeroHeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HEAD_ID); self.view.addSubview(collectionView!); } //MARK: - UICollectionView 代理 //分区数 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 3; } //每个分区含有的 item 个数 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20; } //返回 cell func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_ID, forIndexPath: indexPath) as! MainPageSectionZeroCell; return cell; } //每个分区的内边距 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(10, 10, 10, 10); } //最小 item 间距 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { return 10; } //最小行间距 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { return 10; } //item 的尺寸 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSizeMake(SCREEN_WIDTH / 4.0 - 50 / 4.0, SCREEN_WIDTH / 4.0 - 50 / 4.0); } //每个分区区头尺寸 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSizeMake(SCREEN_WIDTH, 40); } //返回区头、区尾实例 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var headview = MainPageSectionZeroHeadView(); if kind == UICollectionElementKindSectionHeader { headview = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: HEAD_ID, forIndexPath: indexPath) as! MainPageSectionZeroHeadView; } return headview; } //item 对应的点击事件 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("index is \(indexPath.row)"); } }
2. UICollectionViewCell 中添加需要的控件
import Foundationimport UIKitclass MainPageSectionZeroCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame); let image = UIImage(named: "collts"); let imageV = UIImageView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height)); imageV.image = image; self.addSubview(imageV); } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
3.区头 / 区尾 代码实现
import Foundationimport UIKitclass MainPageSectionZeroHeadView: UICollectionReusableView { override init(frame: CGRect) { super.init(frame: frame); self.backgroundColor = UIColor.whiteColor(); let label = UILabel(frame: CGRectMake(0,0,self.frame.width,self.frame.height)); label.text = "标题"; self.addSubview(label); } required init?(coder aDecoder: NSCoder) { fatalError("init(coder) has not been implemented") }}
注:Xib 使用方法与 OC 里面类似,可自行尝试
1 0
- IOS SWIFT---使用UICollectionView
- swift UICollectionView的使用
- Swift UICollectionView 使用
- Swift - 使用网格(UICollectionView)进行流布局
- iOS开发Swift UITableView和UICollectionView的使用
- swift中UICollectionView的使用(headerview/footerview/cell/model)
- Swift自定义UITableView、UICollectionView
- swift 之 UICollectionView
- UICollectionView使用
- UICollectionView 使用
- UICollectionView使用
- UICollectionView使用
- Swift纯代码走进UICollectionView
- [Swift 开发] UICollectionView的用法
- swift 之UICollectionView 与UICollectionViewController
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
- 使用UICollectionView实现一个列表头部拉伸效果(下拉放大)(OC 和 Swift)
- swift UICollectionView 代码创建 和自定义Cell
- 如何阅读文献
- Spring学习-----AOP(1)
- Android 内存泄漏总结
- 京东白条套现让这个冬天不再寒冷
- hive中表的创建以及数据的加载
- Swift UICollectionView 使用
- 设计模式C++版:第十二式桥接模式
- 非对称加密算法(公钥与密钥)
- 斐波那契堆(Fibonacci heap)原理详解
- SQL-子查询
- Visual Code 配置golang开发环境
- 操作系统笔记
- STM32学习--FMSC 和 LCD(1)
- C++(24):STL初步之stack、queue