Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift - 动态添加删除TableView的单元格(以及内部元件-日期控件)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述@H_262_4@ 原帖:http://www.hangge.com/blog/cache/detail_727.html 在Swift开发中,我们有时需要动态的添加或删除列表的单元格。   比如我们做一个消息提醒页面,默认页面只显示两个单元格。当点击第二个单元格(时间标签)时,下面会再添加一个单元格放置日期选择控件(同时新增单元格的高度也会变化)。而再次点击第二个单元格,日期选择控件又会隐藏。     1 2 3

原帖: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
@H_840_197@ 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
@H_376_301@
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 )
}
@H_321_404@didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
//选择cell的row之后
@H_301_419@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
number54 index53 alt1" style="outline:0px!important; BACkground-color:rgb(249,inSection: 0)
.tableView.deleteRowsATindexPaths([indexPathDatePicker],
Fade )
}
}
numberOfSectionsInTableView(tableView: ) -> Int {
return 1
}
//设置cell
number65 index64 alt2" style="outline:0px!important; BACkground-color:rgb(249,cellForRowATindexPath indexPath: )
-> 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 :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
//创建日期选择器
@H_410_@R_944_11235@@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( :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:" :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 : ){
@H_855_674@//更新提醒时间文本框
formatter = NSDateFormatter ()
//日期样式
formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
.dueDateLabel.text = formatter.StringFromDate(datePicker.datE)
}
//根据日期选择器的隐藏与否决定返回的row的数量
number107 index106 alt2" style="outline:0px!important; BACkground-color:rgb(249,numberOfRowsInSection section: {
section == 0 && datePickerVisible{
3
{
number112 index111 alt1" style="outline:0px!important; BACkground-color:rgb(249,numberOfRowsInSection: section)
}
}
//因为日期选择器插入后会引起cell高度的变化,所以要重新设置
number118 index117 alt1" style="outline:0px!important; BACkground-color:rgb(249,
heightForRowATindexPath indexPath: ) -> CGFloat {
//当渲染到达日期选择器所在的cell的时候将cell的高度设为217
indexPath.section == 0 && indexPath.row == 2{
216.0
{
number124 index123 alt1" style="outline:0px!important; BACkground-color:rgb(249,heightForRowATindexPath: indexPath)
}
}
//当覆盖了静态的cell数据源方法时需要提供一个代理方法
//因为数据源对新加进来的日期选择器的cell一无所知,所以要使用这个代理方法
number130 index129 alt1" style="outline:0px!important; BACkground-color:rgb(249,
indentationLevelForRowATindexPath indexPath: {
indexPath.section == 0 && indexPath.row == 2{
//当执行到日期选择器所在的indexPath就创建一个indexPath然后强插
newIndexPath = (forRow: 0,inSection: indexPath.section)
number135 index134 alt2" style="outline:0px!important; BACkground-color:rgb(249,indentationLevelForRowATindexPath: newIndexPath)
{
number137 index136 alt2" style="outline:0px!important; BACkground-color:rgb(249,indentationLevelForRowATindexPath: indexPath)
}
}
}

@H_376_301@

大佬总结

以上是大佬教程为你收集整理的Swift - 动态添加删除TableView的单元格(以及内部元件-日期控件)全部内容,希望文章能够帮你解决Swift - 动态添加删除TableView的单元格(以及内部元件-日期控件)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
猜你在找的Swift相关文章