Swift如何使用表格组件实现单列表
发表于:2025-02-20 作者:千家信息网编辑
千家信息网最后更新 2025年02月20日,本篇文章给大家分享的是有关Swift如何使用表格组件实现单列表,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1、样例说明:(1)列表内容
千家信息网最后更新 2025年02月20日Swift如何使用表格组件实现单列表
本篇文章给大家分享的是有关Swift如何使用表格组件实现单列表,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
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 ---
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() }}
以上就是Swift如何使用表格组件实现单列表,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
单元
表格
形式
方法
组件
相同
内容
按钮
数据
文件
更多
机制
知识
篇文章
视图
复用
实用
普通
也就是
事件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
慕义互联网科技有限公司面试
搭建中转服务器 端口转发
四川超频服务器销售价格
第一次对数据库进行备份必须是
服务器断电后有电池保护
软件开发设计好找工作吗
软件开发工作计划与展望
区局网络安全应急预案
wlan与5g交互网络技术
中行软件开发中心什么部门好
民办的计算机软件开发地址
万樱网络技术有限公司电话
大学网络安全教育横幅
网络安全行业证书重要
牛气霸屏服务器配置
手机app数据库
大数据库技术怎么样
master主服务器工作内容
罗斯文数据库数据表
软件开发报价 每日费用
怀旧服服务器人口比例多少算平衡
睿视点歌系统服务器
杭州九州网络技术有限公司
山西省分布式服务器云服务器
杭州新瑞云软件开发有限公司
嵌入式软件开发书籍推荐
远程网络技术中心建设方案
计算机网络技术的职业性格
中国互联网企业没科技含量
电脑上如何开一个服务器