Xcode9学习笔记49
来源:互联网 发布:哪里有淘宝主播招聘 编辑:程序博客网 时间:2024/05/16 15:56
import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {//表格视图数据源协议、表格视图代理协议 var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]//创建数组作为表格的数据来源 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let rect = CGRect(x: 0, y: 40, width: 320, height: 420)//创建一个显示区域 let tableView = UITableView(frame: rect)//初始化一个表格视图,并设置其位置和尺寸 tableView.delegate = self//设置表格视图的代理为当前的视图控制器类 tableView.dataSource = self//设置表格视图的数据源为当前的视图控制器类 tableView.setEditing(true, animated: false)//在默认状态下开启表格的编辑模式 self.view.addSubview(tableView)//将表格视图添加到当前视图控制器的根视图中 } //添加一个代理方法,用来设置表格视图拥有的行数 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return months.count } //添加一个代理方法,用来初始化或复用表格视图中的单元格 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let identifier = "reusedCell"//创建一个字符串,作为单元格的复用标识符 //单元格的标识符可以看作是一种复用机制,此方法可以从所有已经开辟内存的单元格里面,选择一个具有同样标识符的、空闲的单元格 var cell = tableView.dequeueReusableCell(withIdentifier: identifier) //如果在可重用单元格队列中,没有可以重复使用的单元格,则创建新的单元格。新单元格拥有一个复用标识符 if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier) } let rowNum = indexPath.row//获取当前单元格在段落中的行数 cell?.textLabel?.text = months[rowNum] return cell!//返回设置好的单元格对象 } //添加一个代理方法,用来设置单元格的编辑模式为插入模式 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.insert } //添加一个代理方法,用来响应单元格的插入事件 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCellEditingStyle.insert {//判断如果编辑模式为插入,则执行之后的代码 let rowNum = indexPath.row//获取插入位置的单元格在段落中的行数 months.insert("Honey Moon", at: rowNum)//往数组中同步插入数据,及时更新数据源 let indexPaths = [indexPath]//创建一个包含待插入单元格位置信息的数组 //接着往表格视图中的指定位置,插入新单元格 tableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.right) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }}
阅读全文
0 0
- Xcode9学习笔记49
- Xcode9学习笔记1
- Xcode9学习笔记2
- Xcode9学习笔记3
- Xcode9学习笔记4
- Xcode9学习笔记5
- Xcode9学习笔记6
- Xcode9学习笔记7
- Xcode9学习笔记8
- Xcode9学习笔记9
- Xcode9学习笔记10
- Xcode9学习笔记11
- Xcode9学习笔记12
- Xcode9学习笔记13
- Xcode9学习笔记14
- Xcode9学习笔记15
- Xcode9学习笔记16
- Xcode9学习笔记17
- 做技术Leader要有危机意识
- 有人的地方就有江湖,有江湖就有恩怨,人就是恩怨,程序员也是人
- ReactiveCocoa入门教程
- 《Effective Go》--defer
- CAP原理和数据高可用性
- Xcode9学习笔记49
- 「算法精解_C语言描述」链表_循环链表的实现与分析
- hadoop2.7.3在centos7上部署安装(单机版)
- 27. Remove Element
- Xcode9学习笔记50
- Android五大布局介绍&属性设置大全
- 传统线程的创建
- Banner轮播图的使用1.4.9
- 《七点三刻》有价值的新闻早餐 2017年11月11日星期六,第002期