如何在普通 UIViewController 中使用 UITableView
来源:互联网 发布:java从业人员发展方向 编辑:程序博客网 时间:2024/04/30 00:50
本系列文章 《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 UIKitclass 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。
0 0
- 如何在普通 UIViewController 中使用 UITableView
- 如何在UIViewController中 使用UIRefreshControl
- iOS: 在UIViewController 中添加Static UITableView
- UITableViewController与UIViewController中使用UITableView
- IOS7 UIViewController中使用UIRefreshControl 实现 UITableView下拉刷新
- iOS UIViewController中的UITableView使用UIRefreshControl
- Storyboard中 UIViewController+UITableView使用 autolayout引起的界面布局问题
- 在普通类中使用Spring注入
- 一个UIViewController控制器中,实现多个UITableView的方法
- UIViewController中UITableView对象不响应,急待解决
- UIViewController中UITableView对象不响应,急待解决
- uiviewController管理UITableView
- UIViewController+UITableView 和 UITableViewController
- swift中UIViewController的使用
- 如何使用UITableView
- 如何在GridView的行中应用普通Button.
- 如何在PreferenceActivity中加载普通控件及Preference
- 如何在spring普通的bean中获取session对象
- Linux添加自定义命令方法
- 快速对Mysql添加索引的五个方法
- C# 3D到2D投射
- json
- android动画(一)Interpolator
- 如何在普通 UIViewController 中使用 UITableView
- Controller不是tableView的垃圾桶
- iOS8Extension之Today插件
- 你所不知的 CSS ::before 和 ::after 伪元素用法
- 阅读记录
- android canvas save 和 restore 用法
- vbird笔记2016-02-19
- Android中的人机交互技术
- android中的arrayadapter的三种常用用法