服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - Swift使用表格组件实现单列表

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

2022-07-22 11:19hangge Swift

这篇文章主要为大家详细介绍了Swift使用表格组件实现单列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Swift使用表格组件实现单列表的具体代码,供大家参考,具体内容如下

1、样例说明:

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

2、效果图

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

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 ,  UITableViewDataSource  {
     
     var  ctrlnames:[ String ]?
     var  tableView: UITableView ?
     
     override  func  loadView() {
         super .loadView()
     }
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //初始化数据,这一次数据,我们放在属性列表文件里
         self .ctrlnames =   NSArray (contentsOfFile:
             NSBundle .mainBundle().pathForResource( "Controls" , ofType: "plist" )!)  as ?  Array
         
         print ( self .ctrlnames)
         
         //创建表视图
         self .tableView =  UITableView (frame:  self .view.frame, style: UITableViewStyle . Plain )
         self .tableView!.delegate =  self
         self .tableView!.dataSource =  self
         //创建一个重用的单元格
         self .tableView!.registerClass( UITableViewCell . self ,
             forCellReuseIdentifier:  "SwiftCell" )
         self .view.addSubview( self .tableView!)
         
         //创建表头标签
         let  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
     }
     
     //在本例中,只有一个分区
     func  numberOfSectionsInTableView(tableView:  UITableView ) ->  Int  {
         return  1;
     }
     
     //返回表格行数(也就是返回控件数)
     func  tableView(tableView:  UITableView , numberOfRowsInSection section:  Int ) ->  Int  {
         return  self .ctrlnames!.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
         cell.textLabel?.text =  self .ctrlnames![indexPath.row]
         return  cell
     }
     
     // UITableViewDelegate 方法,处理列表项的选中事件
     func  tableView(tableView:  UITableView , didSelectRowAtIndexPath indexPath:  NSIndexPath )
     {
         self .tableView!.deselectRowAtIndexPath(indexPath, animated:  true )
         
         let  itemString =  self .ctrlnames![indexPath.row]
         
         let  alertController =  UIAlertController (title:  "提示!" ,
             message:  "你选中了【\(itemString)】" , preferredStyle: . Alert )
         let  okAction =  UIAlertAction (title:  "确定" , style: . Default ,handler:  nil )
         alertController.addAction(okAction)
         self .presentViewController(alertController, animated:  true , completion:  nil )
     }
     
     //滑动删除必须实现的方法
     func  tableView(tableView:  UITableView ,
         commitEditingStyle editingStyle:  UITableViewCellEditingStyle ,
         forRowAtIndexPath indexPath:  NSIndexPath ) {
             print ( "删除\(indexPath.row)" )
             let  index = indexPath.row
             self .ctrlnames?.removeAtIndex(index)
             self .tableView?.deleteRowsAtIndexPaths([indexPath],
                 withRowAnimation:  UITableViewRowAnimation . Top )
     }
     
     //滑动删除
     func  tableView(tableView:  UITableView ,
         editingStyleForRowAtIndexPath indexPath:  NSIndexPath )
         ->  UITableViewCellEditingStyle  {
             return  UITableViewCellEditingStyle . Delete
     }
     
     //修改删除按钮的文字
     func  tableView(tableView:  UITableView ,
         titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:  NSIndexPath )
         ->  String ? {
             return  "删"
     }
     
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.hangge.com/blog/cache/detail_552.html

延伸 · 阅读

精彩推荐
  • SwiftSwift教程之继承详解

    Swift教程之继承详解

    这篇文章主要介绍了Swift教程之继承详解,一个类可以从另外一个类中继承方法,属性或者其它的一些特性,当一个类继承于另外一个类时,这个继承的类叫子类...

    Swift 教程网1672020-12-19
  • Swiftswift3.0指纹解锁的实现方法

    swift3.0指纹解锁的实现方法

    这篇文章主要为大家详细介绍了swift3.0指纹解锁的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    zxw_114118919412312021-01-05
  • Swift详解Swift中的下标访问用法

    详解Swift中的下标访问用法

    在Swift中我们可以用subscript函数来定义下标,从而通过下标来访问数组与字典等数据结构,这里我们就来详解Swift中的下标访问用法:...

    珲少8782020-12-28
  • SwiftSwift使用CollectionView实现广告栏滑动效果

    Swift使用CollectionView实现广告栏滑动效果

    这篇文章主要为大家详细介绍了Swift使用CollectionView实现广告栏滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Stevin的技术博客11682021-01-13
  • SwiftRxSwift发送及订阅 Subjects、Variables代码示例

    RxSwift发送及订阅 Subjects、Variables代码示例

    这篇文章主要介绍了RxSwift发送及订阅 Subjects、Variables代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    芬达3852021-01-17
  • SwiftSwift Json实例详细解析

    Swift Json实例详细解析

    这篇文章主要给大家介绍了关于Swift Json解析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    桃红宿雨14012021-01-10
  • SwiftSwift3.0仿支付宝二维码扫描效果

    Swift3.0仿支付宝二维码扫描效果

    这篇文章主要为大家详细介绍了Swift3.0仿支付宝二维码扫描效果的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Stevin三天三夜的专栏10442021-01-03
  • SwiftSwift 3.0基础学习之扩展

    Swift 3.0基础学习之扩展

    扩展是向一个已有的类、结构体或枚举类型添加新的功能(在swift中扩展没有名字)。相当于Objective-C中Category(OC中可以有名字的,而且只能扩展类)。这...

    Mellong9892021-01-04