如何在普通 uiviewcontroller横屏 中使用 UITableView

swift如何在普通 UIViewController 中使用 UITableView
我的图书馆
swift如何在普通 UIViewController 中使用 UITableView
&转自: /ios/429.html本系列文章 《Swift on iOS 学习笔记》 将以不定长度、不定内容、不定形式的方式对外发布,主要记录一些 “可重用” 的知识,感谢你的阅读。在继承自&UIViewController 的普通页面中使用&UITableView 是一种非常普遍的需求,因为&UITableViewController 的可定制性是很差的。话不多说,马上开始:1. 新建 Application2. 添加一个 Table View3. 在 Table View 上添加一个 Table View Cell4. 在左侧选中该&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 {
func numberOfSectionsInTableView(tableView: UITableView) -& Int {
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。WRITTEN BYWeb 程序员,PHP 框架&&作者。iOS & OS X 开发者。相关日志:
TA的最新馆藏[转]&[转]&[转]&[转]&下次自动登录
现在的位置:
& 综合 & 正文
UITableViewController和UIViewController的区别
UITableViewController继承自UIViewController,但是initWithNibName:bundle:方法的行为是不一样的。普通的UIViewController如果nibName参数是nil,则自动载入和自己类名相同的xib文件。而UITableViewController遇到nibName为nil时,却不加载xib文件,而是创建一个空的table view。所以,对于UITableViewController来说,如果使用了xib文件,则必须写出完整xib文件名,才能正确创建。
因此,当使用了xib时,UITableViewController不能使用这样的方式创建:
[[TableViewController alloc] init];
[[TableViewController alloc] initWithNibName:nil bundle:nil];
另外,UITableViewController的view属性和tableView属性是联动的,无法自己改变此种关联。就是说,UITableViewController的顶级view必须是一个tableView,没法自己在loadView中创建一个view,然后再加入一个tableView。所以,如果想自己控制页面的布局,必须继承自UIViewController,而不是UITableViewController。
&&&&推荐文章:
【上篇】【下篇】3233人阅读
ios开发(37)
iOS开发:如何作为子类来创建和管理UITableView
已有 184 次阅读&&
21:38&&&:&&&&&&&&&
在iPhone应用开发中个,常常碰到当用户点击按钮时,需要增加一个表格视图的操作。在开发实现上可以使用在UIViewController中增加一个UITableView视图的方案。
首先从UIViewController派生一个子类类(不需要使用nib文件),在子类loadView方法中装载UITableView,并且程序控制UITableView的大小和位置。代码如下:
// 使用程序创建视图层次关系
- (void)loadView
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
contentView.backgroundColor = [UIColor clearColor];
[self setView:contentView];
[contentView release];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 440) style:UITableViewStylePlain];
[tableView setAutoresizesSubviews:YES];
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[tableView setDataSource:self];
[tableView setDelegate:self];
[[self view] addSubview:tableView];
头文件部分代码
@interface SelectPeopleViewController : UIViewController &UITableViewDelegate, UITableViewDataSource& {
UITableView *tableV
@property (nonatomic, retain) UITableView *tableV
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:269536次
积分:3418
积分:3418
排名:第8757名
原创:74篇
转载:76篇
评论:11条
(1)(1)(4)(1)(2)(10)(14)(27)(15)(5)(12)(28)(9)(2)(4)(3)(1)(2)(1)(2)(5)(1)

我要回帖

更多关于 uiviewcontroller 的文章

 

随机推荐