如何在普通 UIViewController 中使用 UITableView
本系列文章 《Swift on iOS 学习笔记》 将以不定长度、不定内容、不定形式的方式对外发布,主要记录一些 “可重用” 的知识,感谢你的阅读。
在继承自 UIViewController 的普通页面中使用 UITableView 是一种非常普遍的需求,因为 UITableViewController 的可定制性是很差的。话不多说,马上开始:
1. 新建 Application

2. 添加一个 Table View

3. 在 Table View 上添加一个 Table View Cell

4. 在左侧选中该 Table View Cell,并赋予它 Identifier
左侧,点击选中:

右侧,在 Identifier 框里面输入小写的 cell ,输入完成后记得按 Enter 确认:

5. 将该 Table View 绑定到代码

取名为 firstTableView。
6. 修改代码
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var firstTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
firstTableView.delegate = self
firstTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = "我是第 \(indexPath.row) 个Cell"
return cell
}
}
7. 运行:

8. 总结
此 firstTableView 只是页面上一个普通的 view,可以直接调整他大小和位置,也可以随意增加其他 view。
如何在普通 UIViewController 中使用 UITableView的更多相关文章
- iOS: 在UIViewController 中添加Static UITableView
如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...
- 在Storyboard中为UITableView添加Header和Footer
我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...
- UIViewController中各方法调用顺序及功能详解
UIViewController中各方法调用顺序及功能详解 UIViewController中loadView, viewDidLoad, viewWillUnload, viewDidUnload, ...
- 转:UIViewController中各方法调用顺序及功能详解
UIViewController中loadView, viewDidLoad, viewWillUnload, viewDidUnload, viewWillAppear, viewDidAppear ...
- 在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法
许多时候我们都有在普通的继承自 UIViewController 的控制器中使用 TableView 的需求,这时候就需要当前控制器类继承 UITableViewDelegate 和 UITableV ...
- 解决UIViewController中添加子控制器viewWillAppear不调用问题
问题描述: 我在UICollectionViewController中添加子控制器数组, 并在cellForItem中把子控制器数组中对应的控制器对应的view添加到了UICollectionView ...
- iOS 中隐藏UITableView最后一条分隔线
如何优雅的隐藏UITableView中最后一条分割线? 这个问题是很常见,却又不太容易解决的. 可能通常的做法都是隐藏UITableView的分割线,自定义一条. 最近在使用弹出菜单的时候,同样遇到了 ...
- Swift UIViewController中的delegate方式传值
ios swift开发中有几种方式传值,看到简书上一篇不错的文章. 链接:http://www.jianshu.com/p/3e1173652996 一.通过segue进行传值 二.通过delegat ...
- IOS开发中实现UITableView按照首字母将集合进行检索分组
在开发公司项目中遇到了将图书目录进行按照首字母分组排序的问题 1.在项目添加解析汉字拼音的Pinyin.h文件 /* * pinyin.c */ #define HANZI_START 19968 # ...
随机推荐
- The Apache Thrift API client/server architecture
http://thrift.apache.org/ The Apache Thrift software framework, for scalable cross-language services ...
- MongoDB 学习五:索引
这章我们介绍MongoDB的索引,用来优化查询. 索引介绍 数据库索引有些类似书的目录. 一个查询如果没有使用索引被称为表扫描,意思是它必须像阅读整本书那样去获取一个查询结果.一般来说,我们应尽量避免 ...
- 需要注意的一些Mysql语句
1. 日期处理函数:date_format() select COUNT(*) from (SELECT SERIAL_NO, APPLY_SERIAL_NO, FLAG, PAY_DATE, SEQ ...
- HTML,CSS 无边框桌面窗口
1. [图片] htmlui.jpg 2. [代码]下面源码复制到快手(WWW.AAU.CN)中运行即可 import win.ui;/*DSG{{*/var winform = ..win ...
- Mongodb 官网驱动2.2.4.26版本 增,删 改,查,mongodb2.2.4.26
Mongodb是3.2.7版本 最近在学习mongodb数据库在网上找到的都不是2.X版本以下的,因为驱动从2.X以后修改了很多,以前不支持linq现2.X也支持了, Mongodb 启动服务就不说了 ...
- 关于 josnp ,解决跨域问题
JSONP是服务器与客户端跨源通信的常用方法.最大特点就是简单适用,老式浏览器全部支持,服务器不用做任何改造[使用jsonp的时候jsonp: "callback",callbac ...
- 谈谈java中成员变量与成员方法继承的问题
谈谈java中成员变量与成员方法继承的问题 关于成员变量和成员方法的的继承问题,我也可以做一个小测试,来看看结果. 首先我们先创建一个父类:
- Java 高阶 —— try/catch
// try catch 在 for 循环外 try { for(int i = 0; i < max; i++) { String myString = ...; float myNum = ...
- tyvj2044 旅游景点
背景 “扫地”杯III NOIP2012模拟赛 day2 第二题 描述 liouzhou_101住在柳侯公园附近,闲暇时刻都会去公园散散步.很那啥的就是,柳侯公园的道路太凌乱了,假若不认识路就会走着走 ...
- C++之PIMPL模式
1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏. ...