Iphone 是否将UITableView作为子视图?
这可能吗?我只想在我现在的视野里有一张小桌子。。。以下是我想做的: .h文件Iphone 是否将UITableView作为子视图?,iphone,uitableview,subview,addsubview,Iphone,Uitableview,Subview,Addsubview,这可能吗?我只想在我现在的视野里有一张小桌子。。。以下是我想做的: .h文件 #import <UIKit/UIKit.h> @interface IngredientsRootView : UIViewController <UITableViewDelegate, UITableViewDataSource> { UITableView *ingredientsTable; } @property (nonatomic, retain) UITableView
#import <UIKit/UIKit.h>
@interface IngredientsRootView : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *ingredientsTable;
}
@property (nonatomic, retain) UITableView *ingredientsTable;
@end
应用程序不会崩溃,但不会显示表格。目前,我已将所有内容明确设置为:
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
// the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = @"Hello";
return cell;
}
我做错了什么?谢谢在将表视图添加为子视图后,尝试在表视图上调用
-reloadData
另外,视图是如何设置的?它是在XIB中创建的还是通过
-loadView
创建的?您是否记得添加[window addSubview:[IngredientsRootView]]代码>到您的应用程序代理
顺便说一句,您不必从UIViewController继承,只需从UITableViewController继承即可获得所有功能,无需添加代码。无XIBs。。。受不了他们哈。它在viewDidLoad中创建并添加到那里。我试试看。:)我指的是将表视图添加为子视图的视图。这是如何产生的?您是否实现了-loadView
?如果您没有使用XIBs,并且没有实现-loadView
,或者在其他地方设置视图控制器的视图,则当您尝试将表视图添加为子视图时,self.view
将返回nil,并且不会发生任何情况。另外,请记住,viewDidLoad
仅在loadView
之后调用,或者如果视图是在别处设置的或在IB中创建的。在viewDidLoad
中设置一个断点以验证是否调用了它。我不喜欢听上去咄逼人,但这并不能解释视图控制器的视图是在哪里创建的。据我所知,您有一个视图控制器,正在将其推送到堆栈上,并且希望添加一个表视图作为视图控制器管理的视图的子视图?视图控制器需要创建一个视图。这可以通过实现loadView、创建视图实例并将其指定给视图属性、从其他类设置视图属性或在interface builder中创建视图并将其链接到视图出口来实现。你做过这些吗?你是对的(很抱歉回来晚了)。。。问题不在于我没有创建一个视图,而在于我从中构建所有内容-添加了表视图,但在前面的视图(图像视图)后面。将所有内容放入loadView(帮助):)感谢UITableViewController
假设它的视图
属性实际上是一个UITableView
,这使得它是一个不合适的类,如果您的视图是其他视图(例如UIView
“容器”),您是对的-出于某种原因,我认为他的控制器的视图是表视图。无论如何,我的第一个建议仍然有效。谢谢,我想这取决于你在哪里初始化tableView,你在控制器的initWithNib:
方法中做什么?哦,对不起。。。。no viewDidLoad:)尝试将其作为Interface Builder中的IBOutlet链接,并将初始化留给他,只保留委托/数据源部分
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
// the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = @"Hello";
return cell;
}