Swift开发IOS-UITableView
来源:互联网 发布:underscore.js源码 编辑:程序博客网 时间:2024/05/16 06:53
下面通过代理的方法来填充和操作UITableView,需要让UIViewController使用UITableViewDelegate和UITableViewDataSource:
查看UITableViewDataSource协议文件,可以看到UITableViewDataSource有两个方法是必须重写的(required),其它方法是可选(optional):
上面两个require方法必须在ViewController类中实现,否则程序会一直报错:其中func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 函数的返回值是UITableView的item的数量,可以根据section返回每一节(Section)的Item的数量,要是只有一节(Section),则返回全部Item的数量;func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell,UITableViewCell即为每一个Item的布局,indexPath的类型是NSIndexPath,可以获得节序号(indexPath.section)以及这一节(Section)中某一Item的行号(indexPath.row)通过这两个参数返回对应的UITableViewCell。
实现UITableView的步骤:
(一).声明并且托管UITableView的delegate和dataSource
(二).重写UITableViewDelegate和UITableViewDataSource中的方法
[附]其中涉及到的Person.swift和PersonDataSet.swift文件如下:
[效果]
[其他常用功能]
1.UITableViewCell的选择事件
当UITableView中的某一个UITableViewCell被点选的时候,会调用UITableViewDelegate中的func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)-> Void 方法:
2.实现UITableView的编辑功能(Delete和Insert)
2-1 指定每一indexPath的cell是否可以编辑,以及编辑类型
2-2 实现删除和插入的功能
首先在ViewController界面添加一个UIButton,用来切换UITableView的编辑状态:
- 声明编辑按键UIButton:
- 添加按键到ViewController:
- UITableView状态切换:
- UITableViewCellStyle.Default(注意:Default不支持detailTextLabel这个属性)
- UITableViewCellStyle.Subtitle(增加了对detailTextLabel的支持,位于textLabel)
- UITableViewCellStyle.Value1(textLabel居左显示,detailTextLabel居右显示)
- UITableViewCellStyle.Value2(注意:不支持imageView,textLabel居左显示为蓝色 )
- UITableViewCellAccessoryType.None(不显示任何标记按钮)
- UITableViewCellAccessoryType.DisclosureIndicator
- UITableViewCellAccessoryType.DetailDisclosureButton
UITableViewCellAccessoryType.Checkmark
UITableViewCellAccessoryType.DetailButton
- Swift开发IOS-UITableView
- iOS开发Swift UITableView和UICollectionView的使用
- iOS使用Swift创建UITableView
- Swift开发IOS-UILabel
- Swift开发IOS-UITextField
- Swift开发IOS-UIButton
- Swift开发IOS-UITextView
- Swift开发IOS-UISegmentedControl
- Swift开发IOS-UISlider
- Swift开发IOS-UISwitch
- Swift开发IOS-UIStepper
- Swift开发IOS-UIActivityIndicatorView
- Swift开发IOS-UIProgressView
- Swift开发IOS-UIAlertView
- Swift开发IOS-UIActionSheet
- Swift开发IOS-UIImageView
- Swift开发IOS-UIScrollView
- Swift开发IOS-UIPageControl
- ListView的Item中包含RadioButton的使用
- 组合数与排列二叉树
- Matlab的rand(),randn()和randi()函数问题
- 关于UITableView的性能优化(历上最全面的优化分析) ...
- テスト
- Swift开发IOS-UITableView
- Smack+Spark+Openfire集成环境IM开发
- iOS 火星坐标相关整理及解决方案汇总
- k最近邻(KNN)——实践
- git之管理修改
- STM32 FOC Surface Mounted PMSM与Internal PMSM 的区别
- 博客搬家
- Spring学习总结(6)——Spring之核心容器bean
- VMware Workstation for Windows 8.0以上系列版本下载