iOS8自定义Collection View Cell - Swift教程
2016-07-11 17:20
399 查看
Collection View提供了一个灵活方式展示集合视图,用法有点类似
view cell,教程在iOS8&Xcode6下编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写IOS8SwiftCustomCollectionViewCellsTutorial,Organization Name和Organization Identifier自行填写,选择Swift语言与iPhone设备。
我们需要在自定义的Collection View Cell中展示一张图,点击这里下载图片并添加至工程
打开Storyboard,移除ViewController并拖拽
View Controller然后到Attribute Inspector控制面板勾选“Is Initial View Controller”选项,如下图:
选中Collection View同时打开Size Inspector面板,将Cell的大小设置为50x50
拖拽Image View至Collection View Cell中并确保高宽都为50,选中Image View并到Attribute Inspector选择Mode为“Aspect Fit”
现在Storyboard大致如下:
这时
现在我们将新建的类与Storyboard的Collection View Controller 进行关联,打开Storyboard且选中Collection View Controller,然后到Identity Inspector控制面板改变Custrom Class为新建的类,如下图:
打开CollectionViewController.swift文件,在viewDidLoad方法中删除如下行
打开Storyboard选中Collection View Cell设置Identifier为"Cell"
接下来创建Collection View Cell自定义class,在工程中新增一个文件,选择
打开Storyboard选中CollectionViewCell设置自定的Class为
打开Assistant Editor并确保CollectionViewCell.swift 可见,Ctrl+Drag方式给ImageView创建如下Outlet
打开 CollectionViewController.swift 文件增加如下属性
改变如下代码:
1.设置CollectionView的section数量为1
2.Collection View的cells数量为100
3.设置CollectionViewCelll的imageView为myimage
编译并运行项目,效果如下:
原文:http://www.ioscreator.com/tutorials/custom-collection-view-cell-tutorial-ios8-swift
Table View类.使用Collection view可以实现网格或者实现任何你能想象到的布局。在这篇教程中将实现自定义collection
view cell,教程在iOS8&Xcode6下编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写IOS8SwiftCustomCollectionViewCellsTutorial,Organization Name和Organization Identifier自行填写,选择Swift语言与iPhone设备。
我们需要在自定义的Collection View Cell中展示一张图,点击这里下载图片并添加至工程
打开Storyboard,移除ViewController并拖拽
collection view Controller至界面.由于我们移除了初始的ViewController,因此默认启动的界面没了,选中Table
View Controller然后到Attribute Inspector控制面板勾选“Is Initial View Controller”选项,如下图:
选中Collection View同时打开Size Inspector面板,将Cell的大小设置为50x50
拖拽Image View至Collection View Cell中并确保高宽都为50,选中Image View并到Attribute Inspector选择Mode为“Aspect Fit”
现在Storyboard大致如下:
这时
ViewController.swift已经不需要了我们将它删除掉。接下来,在工程中新增一个文件,选择
iOS->Source->Cocoa Touch Class,新建一个继承UICollectionViewController名为CollectionViewController类:
现在我们将新建的类与Storyboard的Collection View Controller 进行关联,打开Storyboard且选中Collection View Controller,然后到Identity Inspector控制面板改变Custrom Class为新建的类,如下图:
打开CollectionViewController.swift文件,在viewDidLoad方法中删除如下行
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
打开Storyboard选中Collection View Cell设置Identifier为"Cell"
接下来创建Collection View Cell自定义class,在工程中新增一个文件,选择
iOS->Source->Cocoa Touch Class,新建一个继承UICollectionViewCell名为
CollectionViewCell类
打开Storyboard选中CollectionViewCell设置自定的Class为
CollectionViewCell
打开Assistant Editor并确保CollectionViewCell.swift 可见,Ctrl+Drag方式给ImageView创建如下Outlet
打开 CollectionViewController.swift 文件增加如下属性
var myImage = UIImage(named: "Apple_Swift_Logo")
改变如下代码:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { // 1 // Return the number of sections return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // 2 // Return the number of items in the section return 100 } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { // 3 let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell // Configure the cell cell.imageView.image = myImage return cell }
1.设置CollectionView的section数量为1
2.Collection View的cells数量为100
3.设置CollectionViewCelll的imageView为myimage
编译并运行项目,效果如下:
原文:http://www.ioscreator.com/tutorials/custom-collection-view-cell-tutorial-ios8-swift
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法