Swift - 动态添加删除TableView的单元格(以及内部元件)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
import UIKitclass MyTableViewController: UITableViewController { @IBOutlet weak var dueDateLabel: UILabel! //日期选择器显示状态 var datePickerVisible:Bool = false override func viewDidLoad() { super.viewDidLoad() self.title = "添加任务" //去除尾部多余的空行 self.tableView.tableFooterView = UIView(frame:CGRectZero) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //选择cell的row之后 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.tableView.deselectRowAtIndexPath(indexPath, animated: true) //当执行到日期选择器上一行的时候,可以判断是否要显示日期选择器了 if indexPath.section == 0 && indexPath.row == 1{ if !datePickerVisible{ self.showDatePicker() }else{ self.hideDatePicker() } } println(indexPath.row) } //显示日期选择器 func showDatePicker(){ //日期选择器的状态设为打开 datePickerVisible = true let indexPathDatePicker = NSIndexPath(forRow: 2, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPathDatePicker], withRowAnimation: UITableViewRowAnimation.Automatic) } //隐藏日期选择器 func hideDatePicker(){ if datePickerVisible { //日期选择器的状态设为关闭 datePickerVisible = false let indexPathDatePicker = NSIndexPath(forRow: 2, inSection: 0) self.tableView.deleteRowsAtIndexPaths([indexPathDatePicker], withRowAnimation: UITableViewRowAnimation.Fade) } } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } //设置cell override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //因为日期选择器的位置在日期显示Label下面。它的位置就是第2个section 和第3个row if indexPath.section == 0 && indexPath.row == 2{ //用重用的方式获取标识为DatePickerCell的cell var cell = tableView.dequeueReusableCellWithIdentifier("DatePickerCell") as UITableViewCell? //如果没找到就创建一个 if cell == nil { //创建一个标识为DatePickerCell的cell cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "DatePickerCell") //设置cell的样式 cell?.selectionStyle = UITableViewCellSelectionStyle.None //创建日期选择器 var datePicker = UIDatePicker(frame: CGRectMake(0.0, 0.0, 320.0, 216.0)) //给日期选择器的tag datePicker.tag = 100 //将日期选择器区域设置为中文,则选择器日期显示为中文 datePicker.locale = NSLocale(localeIdentifier: "zh_CN") //将日期选择器加入cell cell?.contentView.addSubview(datePicker) //注意:action里面的方法名后面需要加个冒号“:” datePicker.addTarget(self, action: "dateChanged:", forControlEvents: UIControlEvents.ValueChanged) } return cell! }else{ return super.tableView(tableView, cellForRowAtIndexPath: indexPath) } } //日期选择器响应方法 func dateChanged(datePicker : UIDatePicker){ //更新提醒时间文本框 let formatter = NSDateFormatter() //日期样式 formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss" self.dueDateLabel.text = formatter.stringFromDate(datePicker.date) } //根据日期选择器的隐藏与否决定返回的row的数量 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 && datePickerVisible{ return 3 }else{ return super.tableView(tableView, numberOfRowsInSection: section) } } //因为日期选择器插入后会引起cell高度的变化,所以要重新设置 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { //当渲染到达日期选择器所在的cell的时候将cell的高度设为217 if indexPath.section == 0 && indexPath.row == 2{ return 216.0 }else{ return super.tableView(tableView, heightForRowAtIndexPath: indexPath) } } //当覆盖了静态的cell数据源方法时需要提供一个代理方法。 //因为数据源对新加进来的日期选择器的cell一无所知,所以要使用这个代理方法 override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int { if indexPath.section == 0 && indexPath.row == 2{ //当执行到日期选择器所在的indexPath就创建一个indexPath然后强插 let newIndexPath = NSIndexPath(forRow: 0, inSection: indexPath.section) return super.tableView(tableView, indentationLevelForRowAtIndexPath: newIndexPath) }else{ return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath) } }} |
Swift - 动态添加删除TableView的单元格(以及内部元件)的更多相关文章
- 用Javascript动态添加删除HTML元素实例 (转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 编辑 Ext 表格(一)——— 动态添加删除行列
一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除. (1) 动态添加表格的行 gridS ...
- js实现网页收藏功能,动态添加删除网址
<html> <head> <title> 动态添加删除网址 </title> <meta charset="utf-8"&g ...
- JS动态生成表格后 合并单元格
JS动态生成表格后 合并单元格 最近做项目碰到表格中的单元格合并的问题,需求是这样的,首先发ajax请求 请求回来后的数据 动态生成表格数据,但是生成后如果编号或者(根据其他的内容)有相同时,要合并单 ...
- Swift动态添加UIImageView并添加事件
Swift动态添加UIImageView并添加事件: 1. 创建UIImageView实例,并进行初始化 2. 设置UIImageView的用户交互属性userInteractionEnabled为T ...
- jquery动态添加删除div--事件绑定,对象克隆
我想做一个可以动态添加删除div的功能.中间遇到一个问题,最后在manong123.com开发文摘 版主的热心帮助下解答了(答案在最后) 使用到的jquery方法和思想就是:事件的绑定和销毁(unbi ...
- jQuery动态添加删除CSS样式
jQuery框架提供了两个CSS样式操作方法,一个是追加样式addClass,一个是移除样式removeClass,下面通过一个小例子讲解用法. jQuery动态追加移除CSS样式 <!DOCT ...
- JS动态添加删除html
本功能要求是页面传一个List 集合给后台而且页面可以动态添加删除html代码需求如下: 下面是jsp页面代码 <%@ page language="java" pageEn ...
- JS-DOM 综合练习-动态添加删除班级成绩表
费了2个小时,才把原理弄懂,把问题逐个解决,当你发现你最后栽的那个点,是一个小石头拌的你,你起来是该哭还是该笑呢?只怪自己习武不精吧. 虽然问题都解决了,但是还有一个余留的问题就是鼠标经过input时 ...
随机推荐
- Android 中 ListView 常用属性合集
class ListView.FixedViewInfo//用来在列表内展现一个固定位置视图,如在列表顶端的header和在列表底端的footer 一.XML属性 1.ListView的XML属性 a ...
- CentOS6.5下VNC Server远程桌面配置详解
参考文献: (总结)CentOS Linux下VNC Server远程桌面配置详解 远程桌面连接工具VNC——license Key 我的下载地址为 太平洋下载 VNC连接黑屏的问题 centos 6 ...
- HTTP 301 跳转和302跳转的区别
常用的重定向方式有: 301 redirect, 302 redirect 与 meta fresh: 301 redirect::301代表永久性转移(Permanently Moved),301重 ...
- Net::OpenSSH 模块使用
use Net::OpenSSH; my $host = "$ip"; my $user = 'root'; my $passphrase = 'uxxxxxD'; my $key ...
- 对base-adapter-helper的简单分析
在微博上看到了这篇Android ListView适配器应该这样写,受益匪浅. 于是依据文章结尾的介绍来到了base-adapter-helper的github,地址:https://github.c ...
- QQ音乐产品经理黄楚雄:产品与用户的情感联系
QQ 音乐产品经理关于产品的一些感悟. 2014 年是 QQ 音乐的第十个产品年度,这十年我们跟用户一起见证了整个互联网音乐的发展.2011 年的 3 月 QQ 音乐公布了第一个 iPhone 平台的 ...
- IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例
IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...
- unix网络编程之listen()详解
转自于:http://blog.csdn.net/ordeder/article/details/21551567 Unix网络编程描述如下: #include <sys/socket.h> ...
- C++逗号运算符与逗号表达式
C++将赋值表达式作为表达式的一种,使赋值操作不仅可以出现在赋值语句中,而且可以以表达式形式出现在其他语句(如输出语句.循环语句等)中.这是C++语言灵活性的一种表现. 请注意,用cout语句输出一个 ...
- C中的链接属性及作用域
如果相同的标识符出现在几个不同的源文件中时,它们是表示相同的实体,还是不同的实体.标识符的链接属性决定如何处理在不同文件中出现的标识符.标识符的作用域与它的链接属性有关. 链接属性一般有三种:exte ...