gpt4 book ai didi

iphone - UITableView 作为 subview ?

转载 作者:行者123 更新时间:2023-12-03 19:46:24 27 4
gpt4 key购买 nike

这可能吗?我只想在当前 View 中放置一张小 table ...这就是我想要做的:

.h 文件

#import <UIKit/UIKit.h>


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

@property (nonatomic, retain) UITableView *ingredientsTable;

@end

.m 文件我有委托(delegate)和数据源方法以及以下代码:

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

我做错了什么?谢谢

最佳答案

将 TableView 添加为 subview 后,尝试在 TableView 上调用-reloadData

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

关于iphone - UITableView 作为 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com