Swift - 使用storyboard创建表格视图(TableViewController)
项目创建完毕后,默认是使用ViewController作为主界面视图。下面通过样例演示,如何使用TableViewController作为主界面视图,同时演示如何在storyboard中设置表格及内部单元格样式。

|
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
|
import UIKitclass MainController: UITableViewController { var tasks:[String] = ["今天任务","明天任务","后天任务"] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tasks.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("maincell", forIndexPath: indexPath) as UITableViewCell //获取label let label = cell.viewWithTag(1000) as UILabel //设置label内容 label.text = "\(indexPath.row):\(tasks[indexPath.row])" return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //获取cell let cell = tableView.cellForRowAtIndexPath(indexPath) //根据原先状态,改变勾选或取消勾选状态 if cell?.accessoryType == UITableViewCellAccessoryType.None{ cell?.accessoryType = UITableViewCellAccessoryType.Checkmark }else{ cell?.accessoryType = UITableViewCellAccessoryType.None } //取消选中状态 tableView.deselectRowAtIndexPath(indexPath, animated: true) }} |
7,上述操作完毕后会发现,表格顶着最上面不好看。我们可以在头部添加一个Navigation
Controller导航控制器。即选中storyboard中的主界面,然后从XCode的顶部菜单选择Editor->Embed
In->Navigation Controller。最后,选择主界面,将title设置为“任务列表” 
Swift - 使用storyboard创建表格视图(TableViewController)的更多相关文章
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- swift:创建表格UITableView
用swift创建单元格和用iOS创建单元格形式基本相同,就是语法上有些异样.swift中调用成员方法不再使用[ ]来发送消息,而是使用.成员方法的形式调用成员函数.这种格式非常类似于java中的点成员 ...
- swift:创建集合视图UICollectionView
swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...
- swift:创建滚动视图的图片轮播器
用swift创建图片轮播器和用OC创建的方式是一样的,都主要用到UIScrollView和UIImageview这两个控件,有几张图片,就将滚动视图的内容区域大小设置为每一张图片的大小乘以张数即可.然 ...
- [Swift实际操作]九、完整实例-(7)登录页面:创建自定义视图及相关组件Swift实际操作
本文将开始创建登录页面,首先创建该页面所需的一些自定义组件:做为登录按钮的自定义视图对象.在[RegLogin]组的名称上点击鼠标右键,打开右键菜单.[New File]->[Cocoa Tou ...
- iOS之创建表格类视图WBDataGridView
项目中创建表格, 引用头文件 #import "WBDataGridView.h" - (void)viewDidLoad{ [superviewDidLoad]; // Do a ...
- Swift语言Storyboard教程:第一部分
更新记录: 该Storyboard教程由Caroline Begbie更新iOS 8和Swift相关内容.原文作者为教程编纂组的成员Matthijs Hollemans. 2014/12/10更新: ...
- Swift Swift语言Storyboard教程:第二部
本文由CocoaChina翻译小组@TurtleFromMars翻译自raywenderlich,原文:Storyboards Tutorial in Swift: Part 2 更新记录:该Stor ...
- Swift语言Storyboard教程:第二部
本文由CocoaChina翻译小组@TurtleFromMars翻译自raywenderlich,原文:Storyboards Tutorial in Swift: Part 2 更新记录:该Stor ...
随机推荐
- Android学习笔记:adb 与 adb shell操作 以及中文乱码解决
1.安装app >adb install xxx.apk 2.卸载app >adb uninstall app的包路径 如:>adb uninstall com.example.my ...
- svn密码问题
官方书籍version control with svn提到了这个问题: Disabling Password Caching When you perform a Subversion operat ...
- atlas z 轴
问题源自一个帖子,因为上传的图比较多,就另开了这个贴写下自己的试验结果,原帖在下面链接中 http://game.ceeger.com/forum/read.php?tid=8911#info NGU ...
- RelativeLayout的属性详解
1. android:layout_below="@+id/first" //在某元素的的下方: android:layout_alignBottom="@+id/fir ...
- [转]CentOS下性能监测工具 dstat
原文链接:http://www.bkjia.com/Linuxjc/935113.html 参考链接:https://linux.cn/article-3215-1.html,http://lhfli ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
- css sprites 图片精灵自动生成 插件
grunt-spritesmith https://www.npmjs.com/package/grunt-spritesmith
- UITableViewHeaderFooterView的使用+自己主动布局
UITableViewHeaderFooterView的使用+自己主动布局 使用UITableView的header或footer复用时,假设採用自己主动布局,你会发现有约束冲突,以下这样写能够消除约 ...
- 半透明panel
用API SetLayeredWindowAttributes
- Python,PIL压缩裁剪图片
自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...