0
我在Xcode 4.3.2中尝试以下操作。我创建了一个单一的视图应用程序。我的ViewController
执行UITableViewDelegate
,UITableViewDataSource
。在init中创建UITableView失败
在ViewController.m
:
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 120, 100)
style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
我实现numberOfSectionsInTableView
,numberOfRowsInSection
,cellForRowAtIndexPath
和didSelectRowAtIndexPath
。它工作正常,并在两行显示“欢迎”。 但是,如果我把UITableView
创建在init中,那么它不起作用。
- (id)init
{
self = [super init];
if (self) {
UITableView *tv = [[UITableView alloc] initWithFrame:
CGRectMake(0, 10, 120, 100) style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
}
return self;
}
我想,那是因为你的观点没有被加载。它仍然处于初始化模式,所以自己不知道它自己的tableview或其他子视图。 – Mrunal 2013-06-07 14:01:37