swift表格控件怎么用
发表于:2025-11-18 作者:千家信息网编辑
千家信息网最后更新 2025年11月18日,这篇文章将为大家详细讲解有关swift表格控件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下1、效果图2、该控件(UITableView) 代码注
千家信息网最后更新 2025年11月18日swift表格控件怎么用
这篇文章将为大家详细讲解有关swift表格控件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
具体内容如下
1、效果图
2、该控件(UITableView) 代码注意的地方:
A、ViewController 不单单继承于 UIViewController,还有 UITableViewDelegate,UITableViewDataSource。
B、要自己重新实现UITableView的3个方法。分别是:numberOfSectionInTableView(table:UITableView), tableView(table:UITableView, numberOfRowsInSetion section:Int), tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
3、我使用的Xcode版本:7.2;
4、完整 源码
import UIKitclass ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{ let ctrlName = ["按钮", "标签", "文本框", "提示框", "开关按钮"]; var tableView:UITableView?; override func loadView() { super.loadView(); } override func viewDidLoad() { super.viewDidLoad() /// 创建视图 tableView = UITableView(frame: view.frame, style: .Plain); tableView!.dataSource = self; tableView!.delegate = self; tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell"); view.addSubview(tableView!); /// 创建标签 let headerLabel = UILabel(frame: CGRectMake(0, 0, 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); /// 在tableView的顶部添加该label tableView!.tableHeaderView = headerLabel; } /// 设置分区 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } /// 返回表格行数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return ctrlName.count; } /// 创建单元格显示内容,(创建indexPath指定的单元) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { /// 为了提高表格显示性能,以创建完成的单元重复使用 let identify = "SwiftCell"; /// 同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath); cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator; cell.textLabel?.text = ctrlName[indexPath.row]; return cell; } /// UITableViewDelegate方法,处理列表项选中事件 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true); let itemString = ctrlName[indexPath.row]; /// 创建提示框 let alertView = UIAlertController(title: "提示", message: "你选中了\(itemString)", preferredStyle: .Alert); /// 向提示框中增加按钮 let alertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {(action)->Void in}); alertView.addAction(alertAction); presentViewController(alertView, animated:true, completion:nil); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}关于"swift表格控件怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
控件
表格
单元
提示
内容
按钮
标签
篇文章
方法
更多
不错
实用
事件
代码
地方
属性
常见
形式
性能
效果
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
win服务器安装usb网卡驱动
数据库系统概论答案pdf
第五人格连接不了服务器
在深圳腾讯公司做软件开发
二道区通用网络技术排名靠前
网络安全短视频乱扫码危害大
警务网络安全教育
什么以信息网络技术为手段
linux系统查看数据库
崇川区九天网络技术服务部
海尔软件开发岗位
中国网络安全个人100强
腾讯文档暂时无法连接服务器
web后端用什么软件开发
湖南科奥软件开发有限公司
系统功能管理和数据库管理区别
专业管理软件开发哪家公司好
网络安全题目多选附答案
少儿网络安全问题大全
泰安平台软件开发公司有哪些
网络安全美篇题目
PS和软件开发
怎样将数据库文件拷贝
数据库小数输入
6g和5g网络技术上区别
软件开发逻辑图
sd卡数据库
北京麻将软件开发建设
万方数据库可以查中国博士论文吗
获取网站数据库路径