如何在普通 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 # ...
随机推荐
- Entity Framework(EF)(一)之database first
1.EF简介ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.该框架曾经为.NET Framework的 ...
- 《打造Facebook》
王淮的<打造Facebook>一书不厚,花半天时间轻松读完.书中没有大段的说教,只有近乎流水的陈述.正如作者所说,打造Facebook这本书由巴克伯格来写再合适不过.可惜他至少在近几年内没 ...
- Composite Pattern
1.将对象组合成树形结构以表示“部分--整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 2.Composite 模式结构图 3.实现 #ifndef _COMPONENT_H ...
- 通过Chrome浏览器进行android调试/Remote Debugging on Android with Chrome
The way your web content behaves on mobile can be dramatically different from the desktop experience ...
- python 快速排序 qsort
def qsort(arr, start, end): if start > end: return def partition(arr, start, end): pivot = arr[st ...
- 【C/C++】malloc()
<math.h>文件中对malloc()函数原型: _CRTIMP void * __cdecl malloc(size_t); MSDN中对malloc()的解释: malloc re ...
- [USACO 2008 MAR] 土地购买
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1597 [算法] 首先将所有土地按长为第一关键字 , 宽为第二关键字排序 显然 , 当 ...
- 微信小程序的ajax数据请求wx.request
微信小程序的ajax数据请求,很多同学找不到api在哪个位置,这里单独把小程序的ajax请求给列出来,微信小程序的请求就是wx.request这个api,wx.request(一些对象参数),微信小程 ...
- HDU1083(最大匹配)
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析
参考 FileInputFormat类中split切分算法和host选择算法介绍 以及 Hadoop2.6.0的FileInputFormat的任务切分原理分析(即如何控制FileInputForm ...