微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 使用表格组件UITableView实现单列表

1,样例说明:
(1)列表内容从Controls.plist文件中读取,类型为Array。
(2)点击列表项会弹出消息框显示该项信息。
(3)按住列表项向左滑动,会出现删除按钮。点击删除即可删除该项。

2,效果


3,单元格复用机制:
由于普通的表格视图中对的单元格形式一般都是相同的,所以本例采用了单元格复用机制,可以大大提高程序性能
实现方式是初始化创建UITableView实例时使用registerClass(UITableViewCell.self,forCellReuseIdentifier: "SwiftCell")创建一个可供重用的UITableViewCell。并将其注册到UITableView,ID为SwiftCell。下次碰到形式(或结构)相同的单元就可以直接使用UITableView的dequeueReusableCellWithIdentifier方法从UITableView中取出。

4,示例代码
---ViewController.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
import UIKit
class ViewController : UIViewController , UITableViewDelegate 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, UITableViewDataSource {
var ctrlnames:[ String ]?
tableView: UITableView ?
override func loadView() {
super .loadView()
}
viewDidLoad() {
.viewDidLoad()
//初始化数据,这一次数据,我们放在属性列表文件
self .ctrlnames = NSArray (contentsOfFile:
NSBundle .mainBundle().pathForResource( "Controls" 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,ofType: "plist" )!) as ? Array
print ( .ctrlnames)
//创建表视图
.tableView = UITableView (frame: .view.frame,style: UITableViewStyle . Plain )
.tableView!.delegate = self
.tableView!.dataSource = self
//创建一个重用的单元格
.tableView!.registerClass( UITableViewCell . 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,forCellReuseIdentifier: "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
}
//在本例中,只有一个分区
numberOfSectionsInTableView(tableView: ) -> Int {
return 1;
}
//返回表格行数(也就是返回控件数)
tableView(tableView: 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,numberOfRowsInSection section: {
return .ctrlnames!.count
}
//创建各单元显示内容(创建参数indexPath指定的单元)
tableView(tableView: 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,cellForRowAtIndexPath indexPath: NSIndexPath )
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
identify: = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType disclosureIndicator
cell.textLabel?.text = .ctrlnames![indexPath.row]
cell
}
// UITableViewDelegate 方法,处理列表项的选中事件
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,didSelectRowAtIndexPath indexPath: )
{
.tableView!.deselectRowAtIndexPath(indexPath,animated: true )
itemString = .ctrlnames![indexPath.row]
alertController = UIAlertController (title: "提示!" 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,
message: "你选中了【\(itemString)】" 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,preferredStyle: UIAlertControllerStyle Alert )
okAction = UIAlertAction "确定" UIAlertActionStyle 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,handler: nil )
alertController.addAction(okAction)
.presentViewController(alertController,completion: )
}
//滑动删除必须实现的方法
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,
commitEditingStyle editingStyle: UITableViewCellEditingStyle 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,
forRowAtIndexPath indexPath: ) {
( "删除\(indexPath.row)" )
index = indexPath.row
.ctrlnames?.removeAtIndex(index)
.tableView?.deleteRowsAtIndexPaths([indexPath],
withRowAnimation: UITableViewRowAnimation Top )
}
//滑动删除
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,editingStyleForRowAtIndexPath indexPath: NSIndexPath )
{
return UITableViewCellEditingStyle Delete
}
//修改删除按钮的文字
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,
titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: String ? {
return "删"
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

--- Controls.plist ---
16
<? xml version = "1.0" encoding "UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
< plist "1.0" >
array >
string >标签 UILabel</ >
>文本框 UITextField</ >
>按钮 UIButton</ >
>开关按钮 UISwitch</ >
>分段控件 UISegmentControl</ >
>图像 UIImageView</ >
>进度条 UIProgressView</ >
>滑动条 UiSlider</ >
>警告框 UIAlertView</ >
</ >
plist >

NSArray是Objective-C里的数组类型。Array是Swift里的数组类型。
为了便于使用,这里把NSArray做了类型转换,转成Array。

as 关键字就是起到类型转换的作用。


NSArray是Objective-C原来就有的类型,Array是Swift新增的类型,Array新加许多方法,更便于操作,所以Swift中可以使用Array的尽量使用Array。var ctrlnames:Array? 可以简写成 var ctrlnames:[String]?

原文地址:https://www.jb51.cc/swift/324866.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐