UITableView是app开发中经常使用到的控件,功能非常强大,多用于数据的显示。

以下以一个简单的实例来介绍其基本使用方法。



先建一个project
代码例如以下:


  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  4. private var dataSource: Dictionary<String, [String]>? //定义表格的数据源
  5. private var keyArray: [String]?
  6. private let cellIdef = "zcell"
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. //初始化数据
  12. demoData()
  13.  
  14. var frame = self.view.bounds
  15. frame.origin.y += 20
  16. frame.size.height -= 20
  17.  
  18. //初始化表格
  19. var tableView = UITableView(frame: frame, style: UITableViewStyle.Plain)
  20. //设置重用标志
  21. tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdef)
  22. tableView.tableFooterView = UIView()
  23. tableView.dataSource = self
  24. tableView.delegate = self
  25. self.view.addSubview(tableView)
  26.  
  27. }
  28.  
  29. override func didReceiveMemoryWarning() {
  30. super.didReceiveMemoryWarning()
  31.  
  32. }
  33.  
  34. private func demoData() {
  35. dataSource = ["国家": ["中国", "美国", "法国", "德国", "意大利", "英国", "俄罗斯"],
  36. "种族": ["白种人", "黄种人", "黑种人"]
  37. ]
  38. keyArray = ["国家", "种族"]
  39. }
  40.  
  41. // MARK: - UITableViewDataSource
  42.  
  43. //设置表格的组数
  44. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  45. return keyArray!.count
  46. }
  47.  
  48. //设置表格每组的行数
  49. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. var array = dataSource![keyArray![section]]
  51. return array!.count
  52. }
  53.  
  54. //设置表格的内容
  55. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  56. var cell = tableView.dequeueReusableCellWithIdentifier(cellIdef, forIndexPath: indexPath) as UITableViewCell
  57. var array = dataSource![keyArray![indexPath.section]]
  58. cell.textLabel.text = array![indexPath.row]
  59. return cell
  60.  
  61. }
  62.  
  63. //设置每组的标题
  64. func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
  65.  
  66. {
  67. return keyArray![section]
  68. }
  69.  
  70. //MARK: - UITableViewDelegate
  71.  
  72. func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  73. tableView.deselectRowAtIndexPath(indexPath, animated: true)
  74. }
  75. }

swift的UITableView的使用的更多相关文章

  1. iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战

    UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...

  2. Swift中UITableView的简单使用

    Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...

  3. Swift开发UITableView常用的一些细节知识点介绍

    <code class="objectivec"><strong><span style="font-size:18px;"> ...

  4. Swift 给UITableView 写extension 时 报错 does not conform to protocol 'UITableViewDataSource'

    那是因为你没有实现 数据源和代理方法 实现下就好了 func tableView(_ tableView: UITableView, numberOfRowsInSection section: In ...

  5. [iOS]swift之UITableView添加通过xib创建的headerView坑爹问题

    情景是这样的,我UITableView添加了一个HeaderView,这个HeaderView是通过xib创建,是UIView.出来的结果却出乎意料,UITableView的Cell最顶部的几个被He ...

  6. Swift - UIView,UItableView,Cell设置边框方法

    // 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...

  7. swift实现UItableview上拉下拉刷新模块

    最近用写个项目 发现上拉下拉刷新模块没找到合适的 so 自己写了一个 由于最近忙 教程就不写了 里面有 直接贴地址https://github.com/DaChengTechnology/DCRefr ...

  8. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  9. swift系统学习控件篇:UITableView+UICollectionView

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...

随机推荐

  1. gradle 2.1构建android出现错误的解决方案

    转自:http://www.tuicool.com/articles/YJNJbuA 使用不同版本Gradle构建Andorid 出现Gradle version xxxx is required 坑 ...

  2. 使用tortoisegit访问git@oschina

    转自:http://www.3lian.com/edu/2014/01-03/121350.html 首先,如果你想使用git@oschina ,你的电脑上必须先有git工具:你可以从这里获取谷歌提供 ...

  3. sqlite3 支持的关联查询

    1.支持多表连接,例如 select * from student,class where student.cid=class.id; 2.支持左外连接(left outer join) 例如: se ...

  4. HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  5. HashCode的秘密

    看String源码HashCode的计算方式: public int hashCode() {int h = hash;if (h == 0 && value.length > ...

  6. 几款很厉害的jQuery数字化签名插件(转)

    在浏览器中,我们有很多方式来绘制生成签名效果,并且有很多很棒很智能的jQuery插件.数字化签名是未来的发展方向,正是这个原因我们这里收集并且推荐了四款超棒的jQuery数字化签名插件,希望大家喜欢! ...

  7. C#实现两个数据库之间的数据上报

    用VS2008实现本地数据库上传数据到远程数据.数据能够是一个表,或一个表的部分数据.或查询数据.或数据编辑后上传. 其他VS版本号.复制当中代码就能够.未使用其他不论什么插件.有具体凝视. 单独页面 ...

  8. 依据硬件设备配置高性能的Nginx

    Nginx的高级配置会涉及硬件,假设配置不好,会直接让性能下降好多好多. 我这里总结一下,怎样依据server的硬件设备来配置Nginx. 见下图: 低訪问量的网络,能够这样配置. 标准的网络訪问量, ...

  9. 【转】四大机器学习降维算法:PCA、LDA、LLE、Laplacian Eigenmaps

    最近在找降维的解决方案中,发现了下面的思路,后面可以按照这思路进行尝试下: 链接:http://www.36dsj.com/archives/26723 引言 机器学习领域中所谓的降维就是指采用某种映 ...

  10. 异类的Javascript处理和解析URL的方式

    通常来说,我们使用Javascript处理和解析URL是使用location对象.在今天这个代码小技巧中,我们使用另外一个比较异类的方式处理和解析URL. 代码如下: function parseUR ...