3

这可能吗?我只想在我当前的视图中有一张小桌子......这就是我想要做的:

.h 文件

#import <UIKit/UIKit.h>


@interface IngredientsRootView : UIViewController <UITableViewDelegate, UITableViewDataSource> {
 UITableView *ingredientsTable;
}

@property (nonatomic, retain) UITableView *ingredientsTable;

@end

.m 文件我有委托和数据源方法以及这段代码:

ingredientsTable = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 300) style:UITableViewStylePlain];
 [ingredientsTable setDelegate:self];
 [ingredientsTable setDataSource:self];
 [self.view addSubview:ingredientsTable];

该应用程序不会崩溃,但不会显示表格。目前,我刚刚将所有内容都设置为:

#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;
}

我究竟做错了什么?谢谢

4

2 回答 2

3

将其添加为子视图后尝试调用-reloadData表视图。

另外,视图是如何设置的?它是在 XIB 中还是通过创建的-loadView

于 2010-08-24T11:41:49.983 回答
0

您是否记得将 添加[window addSubview:[IngredientsRootView view]];到您的应用程序委托?

顺便说一句,您可以从 UITableViewController 继承,而不是从 UIViewController 继承,您将获得所有这些功能,而无需添加代码。

于 2010-08-24T12:08:38.223 回答