Swift - 动态添加删除TableView的单元格以及内部元件-日期控件

原帖:http://www.hangge.com/blog/cache/detail_727.html

在Swift开发中,我们有时需要动态的添加删除列表的单元格。


比如我们做一个消息提醒页面页面显示两个单元格。当点击第二个单元格(时间标签)时,下面会再添加一个单元格放置日期选择控件(同时新增单元格的高度也会变化)。而再次点击第二个单元格,日期选择控件又会隐藏。


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 !
//日期选择器显示状态
datePickerVisible: Bool = false
override func viewDidLoad() {
super .viewDidLoad()
self .title = "添加任务"
//去除尾部多余的空行
.tableView.tableFooterView = UIView (frame: CGRectZero )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
//选择cell的row之后
tableView(tableView: UITableView ,didSelectRowAtIndexPath indexPath: NSIndexPath ) {
.tableView.deselectRowAtIndexPath(indexPath,animated: true )
//当执行到日期选择器上一行的时候,可以判断是否要显示日期选择器了
if indexPath.section == 0 && indexPath.row == 1{
!datePickerVisible{
.showDatePicker()
} else {
.hideDatePicker()
}
}
println (indexPath.row)
}
//显示日期选择器
showDatePicker(){
//日期选择器的状态设为打开
datePickerVisible = true
let indexPathDatePicker = (forRow: 2,inSection: 0)
.tableView.insertRowsAtIndexPaths([indexPathDatePicker],
withRowAnimation: UITableViewRowAnimation . Automatic )
}
//隐藏日期选择器
hideDatePicker(){
datePickerVisible {
//日期选择器的状态设为关闭
false
.tableView.deleteRowsAtIndexPaths([indexPathDatePicker],
Fade )
}
}
numberOfSectionsInTableView(tableView: ) -> Int {
return 1
}
//设置cell
)
-> UITableViewCell {
//因为日期选择器的位置在日期显示Label下面。它的位置就是第2个section 和第3个row
indexPath.section == 0 && indexPath.row == 2{
//用重用的方式获取标识为DatePickerCell的cell
cell = tableView.dequeueReusableCellWithIdentifier( "DatePickerCell" )
as ?
//如果没找到就创建一个
cell == nil {
//创建一个标识为DatePickerCell的cell
cell = (style: UITableViewCellStyle Default ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
reuseIdentifier: )
//设置cell的样式
cell?.selectionStyle = UITableViewCellSelectionStyle None
//创建日期选择器
datePicker = UIDatePicker 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( ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action: "dateChanged:" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
forControlEvents: UIControlEvents ValueChanged )
}
cell!
{
return .tableView(tableView,cellForRowAtIndexPath: indexPath)
}
}
//日期选择器响应方法
dateChanged(datePicker : ){
//更新提醒时间文本框
formatter = NSDateFormatter ()
//日期样式
formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
.dueDateLabel.text = formatter.stringFromDate(datePicker.date)
}
//根据日期选择器的隐藏与否决定返回的row的数量
{
section == 0 && datePickerVisible{
3
{
}
}
//因为日期选择器插入后会引起cell高度的变化,所以要重新设置
heightForRowAtIndexPath indexPath: ) -> CGFloat {
//当渲染到达日期选择器所在的cell的时候将cell的高度设为217
indexPath.section == 0 && indexPath.row == 2{
216.0
{
}
}
//当覆盖了静态的cell数据源方法时需要提供一个代理方法
//因为数据源对新加进来的日期选择器的cell一无所知,所以要使用这个代理方法
indentationLevelForRowAtIndexPath indexPath: {
indexPath.section == 0 && indexPath.row == 2{
//当执行到日期选择器所在的indexPath就创建一个indexPath然后强插
newIndexPath = (forRow: 0,inSection: indexPath.section)
{
}
}
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...