我正在尝试以编程方式将 UITableView
添加到现有的 UIViewController
。
我的 run.h
文件有这个:
@interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}
@property (strong, nonatomic) UITableView *tableView;
我的应用程序通过了速度测试,在测试结束时,我尝试向 View 添加一个 TableView。目前我有这个:
-(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 输出中看不到任何错误。
我需要在这里做的任何事情:
当应用程序失败时,我得到的是:
最佳答案
如果 UITableView
的宽度为零,它永远不会调用 datasource
所以像这样更改你的代码
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
祝你好运!
关于ios - 将 UITableView 添加到现有的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844921/