Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签
表格UITableView中,每一单元格都是一个UITableViewCell。其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签。
|
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
|
import UIKitclass 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" //同一形式的单元格重复使用,在声明时已注册 var secno = indexPath.section var data = self.allnames?[secno] if(secno == 0) { var cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator var image = UIImage(named:"heart.jpg") cell.imageView?.image = image cell.textLabel?.text = data![indexPath.row] return cell } else { //第二个分组表格使用详细标签 var adcell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "SwiftCell") adcell.textLabel?.text = data![indexPath.row] println(adcell.textLabel?.text) adcell.detailTextLabel!.text = "这是\(data![indexPath.row])的说明" return adcell; } } // 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 - 给表格的单元格UITableViewCell添加图片,详细文本标签的更多相关文章
- poi 读取word 遍历表格和单元格中的图片
背景 项目需要解析word表格 需要批量导入系统,并保存每行信息到数据库 并且要保存word中的图片, 并保持每条信息和图片的对应关系 一行数据可能有多条图片 解决办法 没有找到现成的代码,怎么办呐? ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:为所有表格的单元格添加边框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮
类似的做法如之前这篇随笔:114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果) 相比之下:自定义 UITableViewCell 的内容灵活,可根据需求 ...
- jquery操作表格 合并单元格
jquery操作table,合并单元格,合并相同的行 合并的方法 $("#tableid").mergeCell({ cols:[X,X] ///参数为要合并的列}) /** * ...
- 使用POI创建word表格合并单元格兼容wps
poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...
- 如何在excel单元格中插入图片批注
在excel单元格中插入图片批注的方法: 1.选定要插入图片的单元格,然后右键选择插入批注. 2.然后会插入一个批注框,为了不影响图片效果,可以将批注文字都删除.然后鼠标移动到批注框边角再右键. 3. ...
- DataGridView单元格显示GIF图片
本文转载:http://home.cnblogs.com/group/topic/40730.html DataGridView单元格显示GIF图片 gifanimationindatagrid.ra ...
- NPOI 在指定单元格导入导出图片
NPOI 在指定单元格导入导出图片 Intro 我维护了一个 NPOI 的扩展,主要用来导入导出 Excel 数据,最近有网友提出了导入 Excel 的时候解析图片的需求,于是就有了本文的探索 导入E ...
- 【Telerik】实现列表单元格中添加复选框,进行状态(是、否)判断
前台界面: 需求:实现对每条细则是否必备进行判断,必备就勾选,否则不勾选. 首先:要保证列表GridView是可编辑的(IsReadOnly=false) 表格代码 其次:单元格的数据绑定要保证是双向 ...
随机推荐
- 富文本编辑器 - wangEditor 插入代码
效果: 项目结构: 注意事项: highlightJS 代码高亮插件,wangEditor 本身就是集成的highlightJS代码高亮插件. 在wangEditor-1.3.12.js里找到var ...
- CF#213DIV2:B The Fibonacci Segment
You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for ...
- Swift中的ViewController
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...
- SDK Hello world(直接使用SDK封装)
前言 将代码拆分了一下, 如果处理更多的消息也不怕看的眼花 SDK编程就是对各种Windows消息的处理 实验工程 /// @file exam_1.cpp /// @brief 查阅本地MSDN, ...
- POJ 2031 prim
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...
- Eclipse用法和技巧十八:减少不必要的输入
写代码的时候,很多人都有一个原则,尽量上输入.依靠IDE自动生成的代码,一般可读性,排版什么的都还是不错的,最主要的一般不会有什么低级错误.今天介绍几个在eclipse环境中,常用的依靠eclipse ...
- openstack ovs-gre 网速慢解决方案
Hint: if you want to check if this answer will solve your issue, execute ifconfig eth0 mtu 1400 on t ...
- The parent project must have a packaging type of POM
在Eclipse中使用Maven添加模块时报错:The parent project must have a packaging type of POM 解决办法: 是将pom.xml 中的 < ...
- 详解js和jquery里的this关键字
详解js和jquery里的this关键字 js中的this 我们要记住:this永远指向函数运行时所在的对象!而不是函数被创建时所在的对象.this对象是在运行时基于函数的执行环境绑定的,在全局环境中 ...
- Struts 2中的constant详解
通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. 1.<constant ...