Swift - 使用表格组件(UITableView)实现单列表
(1)列表内容从Controls.plist文件中读取,类型为Array。
forCellReuseIdentifier:
"SwiftCell")创建一个可供重用的UITableViewCell。并将其注册到UITableView,ID为SwiftCell。下次碰到
形式(或结构)相同的单元就可以直接使用UITableView的dequeueReusableCellWithIdentifier方法从
UITableView中取出。
|
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 UIKitclass 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: UIAlertControllerStyle.Alert) let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.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() }} |
--- Controls.plist ---
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" <plist version="1.0"><array> <string>标签 UILabel</string> <string>文本框 UITextField</string> <string>按钮 UIButton</string> <string>开关按钮 UISwitch</string> <string>分段控件 UISegmentControl</string> <string>图像 UIImageView</string> <string>进度条 UIProgressView</string> <string>滑动条 UISlider</string> <string>警告框 UIAlertView</string></array></plist> |
Swift - 使用表格组件(UITableView)实现单列表的更多相关文章
- 编写Java程序,使用JTable表格组件展现人员信息列表
返回本章节 返回作业目录 需求说明: 使用JTable组件显现人员信息列表 实现思路: 创建一个JTable对象. 创建一个JScrollPane对象(显示横向和纵向滚动条). 将表格添加到滚动面板. ...
- Swift - 使用表格组件(UITableView)实现分组列表
1,样例说明: (1)列表以分组的形式展示 (2)同时还自定义分区的头部和尾部 (3)点击列表项会弹出消息框显示该项信息. 2,效果图: 3,代码如下: 1 2 3 4 5 6 7 8 9 ...
- 【react表格组件】react-virtualized虚拟列表
https://css-tricks.com/rendering-lists-using-react-virtualized/
- Vue + Element-ui实现后台管理系统(5)---封装一个Form表单组件和Table表格组件
封装一个Form表单组件和Table组件 有关后台管理系统之前写过四遍博客,看这篇之前最好先看下这四篇博客.另外这里只展示关键部分代码,项目代码放在github上: mall-manage-syste ...
- layui数据表格使用(一:基础篇,数据展示、分页组件、表格内嵌表单和图片)
表格展示神器之一:layui表格 前言:在写后台管理系统中使用最多的就是表格数据展示了,使用表格组件能提高大量的开发效率,目前主流的数据表格组件有bootstrap table.layui table ...
- Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- Ext JS 6学习文档-第5章-表格组件(grid)
Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...
- vue+element 使用Export2Excel导出表格组件
下载表格组件是根据我自己的业务需求来封装的 使用的是vue中 xlsx 的插件,需要安装新的依赖及配置 仅供参考 不保证和你百分百匹配 安装依赖 npm install -S file-saver x ...
- JS组件系列——表格组件神器:bootstrap table
前言:之前一直在忙着各种什么效果,殊不知最基础的Bootstrap Table用法都没有涉及,罪过,罪过.今天补起来吧.上午博主由零开始自己从头到尾使用了一遍Bootstrap Table ,遇到不少 ...
随机推荐
- ant学习记录(复制-移动-删除-依赖综合测试)+fileset
<?xml version="1.0"?> <project name="targetStudy" default="mkdir&q ...
- Android学习笔记:Activity生命周期详解
进行android的开发,必须深入了解Activity的生命周期.而对这个讲述最权威.最好的莫过于google的开发文档了. 本文的讲述主要是对 http://developer.android.co ...
- BNU Questions and answers
http://www.bnuoj.com/bnuoj/problem_show.php?pid=2490 这个题是先输入一个整数n,说明有几个数据,然后输入n个整数,然后用三个#分开,后面输入整数k, ...
- 基于二叉树和数组实现限制长度的最优Huffman编码
具体介绍详见上篇博客:基于二叉树和双向链表实现限制长度的最优Huffman编码 基于数组和基于链表的实现方式在效率上有明显区别: 编码256个符号,符号权重为1...256,限制长度为16,循环编码1 ...
- Android-onInterceptTouchEvent()和onTouchEvent()总结
老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了: 1.onInterceptTouchEvent( ...
- WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下?
原文 WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下? 一.问题的提出 偶然发现,Winform里的WebBrowser和IE实际安装的版本似乎并不同步,很有趣! 下 ...
- c#实现Javascript的encodeURIComponent()函数
原文 c#实现Javascript的encodeURIComponent()函数 国内外各搜索引擎,均用JavaScript的encodeURIComponent()函数对搜索关键字进行编码,终于找 ...
- 操作PDF文档功能的相关开源项目探索——iTextSharp 和PDFBox
原文 操作PDF文档功能的相关开源项目探索——iTextSharp 和PDFBox 很久没自己写写心得日志与大家分享了,一方面是自己有点忙,一方面是自己有点懒,没有及时总结.因为实践是经验的来源,总结 ...
- Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)
想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...
- C#拖曳控件加载,bll报错问题
C#拖曳控件加载,bll报错问题,加载时实例如化bll时加上一个判断 if (!(GetService(typeof(IDesignerHost)) != null || Sys ...