Swift - 使用表格组件(UITableView)实现分组列表
1,样例说明:
(1)列表以分组的形式展示
(1)列表以分组的形式展示
(2)同时还自定义分区的头部和尾部
(3)点击列表项会弹出消息框显示该项信息。
2,效果图:

(3)点击列表项会弹出消息框显示该项信息。
2,效果图:


3,代码如下:
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
|
import UIKit class ViewController : UIViewController , UITableViewDelegate , UITableViewDataSource { var tableView: UITableView ? var allnames: Dictionary < Int , [ String ]>? var adHeaders:[ String ]? override func loadView() { super .loadView() } override func viewDidLoad() { super .viewDidLoad() //初始化数据,这一次数据,我们放在属性列表文件里 self .allnames = [ 0:[ String ]([ "UILabel 标签" , "UITextField 文本框" , "UIButton 按钮" ]), 1:[ String ]([ "UIDatePiker 日期选择器" , "UIToolbar 工具条" , "UITableView 表格视图" ]) ]; println ( self .allnames) self .adHeaders = [ "常见 UIKit 控件" , "高级 UIKit 控件" ] //创建表视图 self .tableView = UITableView (frame: self .view.frame, style: UITableViewStyle . Grouped ) self .tableView!.delegate = self self .tableView!.dataSource = self //创建一个重用的单元格 self .tableView!.registerClass( UITableViewCell . self , forCellReuseIdentifier: "SwiftCell" ) self .view.addSubview( self .tableView!) //创建表头标签 var headerLabel = UILabel (frame: CGRectMake (0, 0, self .view.bounds.size.width, 30)) headerLabel.backgroundColor = UIColor .blackColor() headerLabel.textColor = UIColor .whiteColor() headerLabel.numberOfLines = 0 headerLabel.lineBreakMode = NSLineBreakMode . ByWordWrapping headerLabel.text = "高级 UIKit 控件" headerLabel.font = UIFont .italicSystemFontOfSize(20) self .tableView!.tableHeaderView = headerLabel } //在本例中,有2个分区 func numberOfSectionsInTableView(tableView: UITableView !) -> Int { return 2; } //返回表格行数(也就是返回控件数) func tableView(tableView: UITableView , numberOfRowsInSection section: Int ) -> Int { var data = self .allnames?[section] return data!.count } // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部 func tableView(tableView: UITableView , titleForHeaderInSection section: Int )-> String { var headers = self .adHeaders!; return headers[section]; } // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部 func tableView(tableView: UITableView , titleForFooterInSection section: Int )-> String { var data = self .allnames?[section] return "有\(data!.count)个控件" } //创建各单元显示内容(创建参数indexPath指定的单元) func tableView(tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { //为了提供表格显示性能,已创建完成的单元需重复使用 let identify: String = "SwiftCell" //同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell cell.accessoryType = UITableViewCellAccessoryType . DisclosureIndicator var secno = indexPath.section var data = self .allnames?[secno] cell.textLabel?.text = data![indexPath.row] return cell } // UITableViewDelegate 方法,处理列表项的选中事件 func tableView(tableView: UITableView !, didSelectRowAtIndexPath indexPath: NSIndexPath !) { self .tableView!.deselectRowAtIndexPath(indexPath, animated: true ) var itemString = self .allnames![indexPath.section]![indexPath.row] var alertview = UIAlertView (); alertview.title = "提示!" alertview.message = "你选中了【\(itemString)】" ; alertview.addButtonWithTitle( "确定" ) alertview.show(); } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
分类:
Swift语言
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2014-09-25 Android 出现警告Exported service does not require permission
2013-09-25 Android中的JSON详细总结