Swift - 动态添加删除TableView的单元格(以及内部元件)
删除 以及 添加 动态 swift 内部 单元格 tableView
2023-09-11 14:17:59 时间
在Swift开发中,我们有时需要动态的添加或删除列表的单元格。
![](http://www.hangge.com/blog_uploads/201505/201505140947361752.png)
比如我们做一个消息提醒页面,默认页面只显示两个单元格。当点击第二个单元格(时间标签)时,下面会再添加一个单元格放置日期选择控件(同时新增单元格的高度也会变化)。而再次点击第二个单元格,日期选择控件又会隐藏。
![](http://www.hangge.com/blog_uploads/201505/2015051409472697227.png)
![](http://www.hangge.com/blog_uploads/201505/201505140947361752.png)
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 UIKit class 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) } } } |
相关文章
- SharePoint 2013 手动删除爬网项目
- git推送本地分支到远端 以及删除远端分支的 命令
- Google Earth Engine(GEE)——如何再GEE中删除或选定指定的矢量数据列表
- 红黑树插入/删除的几种情况,图论,拓扑学杂烩
- 《Spring Data 官方文档》5.6 保存, 更新, 以及删除数据行
- [新手技巧] 如何在Ubuntu中添加和删除书签
- oracle索引,索引的建立、修改、删除
- C#-文件只读时进行拷贝或删除报错, 对路径不允许访问
- SVN如何删除文件名包含空格的文件
- 多选删除最佳处理
- 如何删除JAVA集合中的元素
- 力扣解法汇总380- O(1) 时间插入、删除和获取随机元素
- Linux之特殊的环境变量IFS以及如何删除带有空格的目录
- SharePoint 2013 批量导入、删除帐号
- 图片右上角添加删除
- C# 删除文件以及文件夹
- mongoDB 删除集合后,空间不释放
- thinkphp 删除所有缓存 Rumtime 以及 Html 静态缓存
- 【Linux】:Linux下创建用户和删除用户
- 达梦数据库配置自动备份和自动删除过期备份文件方法