UITableView 作为子视图?

发布于 2024-09-15 17:53:33 字数 1554 浏览 2 评论 0原文

这可能吗?我只想在当前视图中有一个小表...这是我想要做的:

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

我做错了什么?谢谢

Is this possible? I just want a small table in my current view... here is what I'm trying to do:

.h file

#import <UIKit/UIKit.h>


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

@property (nonatomic, retain) UITableView *ingredientsTable;

@end

.m file I have delegate and data source methods and this code:

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

The app doesn't crash, but it doesn't show a table. At the moment I have just set everything definitely as per:

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

What am I doing wrong? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-09-22 17:53:33

将表视图添加为子视图后,尝试在表视图上调用 -reloadData

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

Try calling -reloadData on the table view after adding it as a subview.

Also, how is the view set up? Is it created in a XIB or via -loadView?

懵少女 2024-09-22 17:53:33

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

顺便说一句,您可以直接从 UITableViewController 继承,而不是从 UIViewController 继承,并且无需添加代码即可获得所有功能。

Did you remembered to add the [window addSubview:[IngredientsRootView view]]; to your app delegate ?

And by the way, instead of inheriting from UIViewController you can just inherit from UITableViewController and you'll get all that functionality for you with no added code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文