以编程方式创建UITableViewController [英] Creating a UITableViewController programmatically

查看:141
本文介绍了以编程方式创建UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我的尝试。屏幕上没有任何内容,并且没有任何你应该实现的UITableView方法被调用。

This is what I tried. Nothing appears on the screen and none of the UITableView methods that you are supposed to implement are getting called.

-(void)loadView 
{
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    self.view = view;
    [view release];
}


- (void)viewDidLoad 
{
    [super viewDidLoad];
    UITableViewController *TVC = [[[UITableViewController alloc] 
    initWithStyle:UITableViewStylePlain] autorelease];
    CGRect cgRct = CGRectMake(0, 10, 320, 100);
    UIView *newView = [[UIView alloc] initWithFrame:cgRct];
    TVC.view = newView;
    [newView release];
    [self.view addSubview:TVC.view];
}

我已经找到了关于以编程方式执行此操作的好例子和教程但是有没有。

I've looked for good examples and tutorials on doing this programmatically but there are none.

我想要实现的是一个表,它会占用我的屏幕。也许3/4的屏幕会很好。

What I am trying to achieve is a Table that doenst take up my who screen. Maybe 3/4 of my screen would be good.

非常感谢
代码

Many Thanks Code

推荐答案

问题是你正在创建一个UITableViewController,它是一个UIViewController,并且预计会在导航堆栈上。你想要做的是创建一个UITable,这是一个UIView。您也没有设置表的委托和数据源,您需要这样做以获得回溯。

The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.

您的viewDidLoad应该看起来像这样

Your viewDidLoad should look something like this

- (void)viewDidLoad 
{
    [super viewDidLoad];
    UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
    table.dataSource = self;
    table.delegate = self;
    table.frame = CGRectMake(0, 10, 320, 100);
    [self.view addSubview:table];
}

(请注意,如果您需要访问以外的表格回调,你应该把它保存在一个ivar而不是在本地声明它,并且应该保留它。如果你需要更多行代码来告诉我你的意思,请告诉我。)

(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)

这篇关于以编程方式创建UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆