注意UITableViewCell的style:
(3)
UITableViewCellStyle.Value2:同Value1位置相同,左边是蓝色文本
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
@H_404_205@
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
@H_751_301@
127
128
129
130
131
132
133
134
135
136
|
import
UIKit
class
ViewController
:
UIViewController
,
UITableViewDelegate
UITableViewDataSource
{
var
tableView:
UITableView
?
allnames:
Dictionary
<
Int
String
]>?
adHeaders:[
String
]?
override
func
loadView() {
super
.loadView()
}
viewDidLoad() {
.viewDidLoad()
self
.allnames = [
0:[
]([
1:[
]([
"UIDatePiker 日期选择器"
@H_403_472@];
"UIToolbar 工具条"
"UITableView 表格视图"
])
print
(
.allnames)
.adHeaders = [
"常见 UIKit 控件"
"高级 UIKit 控件"
]
//创建表视图
.tableView =
(frame:
.view.frame,style:
UITableViewStyle
.
Grouped
)
.tableView!.delegate =
self
.tableView!.dataSource =
self
//创建一个重用的单元格
.tableView!.registerClass(
UITableViewCell
.
"SwiftCell"
)
.view.addSubview(
.tableView!)
//创建表头标签
let
headerLabel =
UILabel
(frame:
CGRectMake
(0,
.view.bounds.size.width,30))
headerLabel.backgroundColor =
UIColor
.blackColor()
headerLabel.textColor =
.whiteColor()
headerLabel.numberOfLines = 0
headerLabel.lineBreakMode =
NSLineBreakMode
ByWordWrapping
headerLabel.text =
"高级 UIKit 控件"
headerLabel.font =
UIFont
.italicSystemFontOfSize(20)
.tableView!.tableHeaderView = headerLabel
}
//在本例中,有2个分区
numberOfSectionsInTableView(tableView:
UITableView
) ->
Int
{
return
2;
}
//返回表格行数(也就是返回控件数)
tableView(tableView:
{
data =
.allnames?[section]
data!.count
}
tableView(tableView:
section:
)->
?
{
headers =
.adHeaders!;
headers[section];
}
section:
?
{
.allnames?[section]
return
"有\(data!.count)个控件"
}
NSIndexPath
)
->
UITableViewCell
{
identify:
=
"SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
secno = indexPath.section
.allnames?[secno]
if
(secno == 0)
{
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as
UITableViewCell
cell.accessoryType =
UITableViewCellAccessoryType
DisclosureIndicator
image =
@H_502_804@cell.imageView?.image = image
UIImage
(named:
"heart.png"
)
cell.textLabel?.text = data![indexPath.row]
cell
}
else
{
//第二个分组表格使用详细标签
adcell =
UITableViewCell
(style:
UITableViewCellStyle
Subtitle
reuseIdentifier:
)
adcell.textLabel?.text = data![indexPath.row]
(adcell.textLabel?.text)
adcell.detailTextLabel!.text =
"这是\(data![indexPath.row])的说明"
adcell;
}
}
// UITableViewDelegate 方法,处理列表项的选中事件
)
{
.tableView!.deselectRowAtIndexPath(indexPath,animated:
true
)
itemString =
.allnames![indexPath.section]![indexPath.row]
alertview =
UIAlertView
();
alertview.title =
"提示!"
alertview.message =
"你选中了【\(itemString)】"
;
alertview.addButtonWithTitle(
"确定"
)
alertview.show();
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_558.html