//1.创建表格

let tv = UITableView(frame: view.bounds, style: .Plain)

//2.添加到视图

view.addSubview(tv)

//3.注册可以使用的cell  [UITableViewCell class] OC

tv.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellID")

//4.设置数据源

//Swift 中没有遵守协议是一个错误

tv.dataSource = self

(遵守协议 UITableViewDataSource)

  注:在Swift中没有 @interface ViewController (),因此在  class ViewController: UIViewController , UITableViewDataSource

实现协议方法:

//MARK:UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 20

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cellID")

//提示:textLabel 是可选的

//代码中?是自动带的,如果textLabel有就使用

cell?.textLabel?.text = "hello world ----\(indexPath.row)"

return cell!

}

最新文章

  1. python之sys模块详解
  2. 微信 {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"}
  3. shell note
  4. Listview和Gridview自定义分割线
  5. C#语言基础02
  6. bzoj2757
  7. 我的开发框架(WinForm)3
  8. (九)groupByKey,reduceByKey,sortByKey算子-Java&Python版Spark
  9. cygwin编译SDL1.2
  10. HDU4127(IDA*)
  11. NPOI操作类
  12. [USACO15OPEN]回文的路径Palindromic Paths 2.0版
  13. JS基础(二)
  14. Leetcode解题-链表(2.2.6)RotateList
  15. 记录几个字符串转html的帮助类,已防忘记
  16. nginx 学习笔记
  17. JAVA设计模式初探之适配器模式
  18. css链接link
  19. 初识MapReduce
  20. linux源码安装 rpm命令

热门文章

  1. 2019-RHCE-红帽题库(稳定)
  2. Go语言资源教程:Redis介绍安装和使用
  3. 转载黑客是如何黑到你手机的?绝对涨姿势,一位黑客的Wi-Fi入侵实录!
  4. 云端搭建Linux学习环境 链接https://edu.aliyun.com/article/19 (阿里云ECS服务器 )课堂
  5. chrome插件-YSlow 一个使用的web性能测试插件
  6. C++ 标准库智能指针
  7. the swap trick用于锐减过剩容量
  8. 浅谈 关于ARC循环引用得问题
  9. js判断字符串是否有下划线
  10. javaEE 转发 和 重定向