将 UITableView 添加到现有的 ViewController [英] Add a UITableView to an existing ViewController
问题描述
我正在尝试以编程方式将 UITableView
添加到现有的 UIViewController
.
I'm trying to add a UITableView
to an existing UIViewController
programmatically.
我的 run.h
文件有这个:
@interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}
@property (strong, nonatomic) UITableView *tableView;
我的应用程序通过了速度测试,在测试结束时,我试图将一个 TableView 添加到视图中.目前我有这个:
My app runs through a speed test and at the end of the test I'm trying to add a TableView to the View. At the moment I have this:
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Testing";
return cell;
}
该应用程序从不进入 cellForRowAtIndexPath
方法,但确实进入了上述其他两个方法.我在 Xcode 输出中看不到任何错误.
The app never makes it into the cellForRowAtIndexPath
method but does make it into the other two above. I can't see any error in the Xcode output.
我需要在这里做的任何事情:
Anything I need to be doing here:
当应用程序失败时,我得到的是:
When the app fails all I get is this:
推荐答案
如果 UITableView
的宽度为零,它将永远不会调用 datasource
If width of UITableView
is zero, it will never call datasource
所以像这样改变你的代码
So change your code like this
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
祝你好运!
这篇关于将 UITableView 添加到现有的 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!