将UITableView添加到现有的ViewController
我试图以编程方式将UITableView
添加到现有的UIViewController
。
我的run.h
文件有这样的:
@interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;} @property (strong, nonatomic) UITableView *tableView;
我的应用程序运行速度testing,并在testing结束时,我试图添加一个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;
祝你好运!
你应该用一个框架(也可以是一个样式)来初始化你的UITableView,或者至less在某个地方定义框架。
例如:
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
您需要将属性tableView
分配给在addSpeedTable
方法中创build的实例,然后使用self.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]; }
编辑
正如其他答案所指出的那样,您可能还需要在UITableView
上设置框架:
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
你使用init方法初始化视图,这意味着视图的框架是CGRectZero。 所以调用numberOfRowsInSection,但SDK发现没有细胞需要显示,因为框架是CGRectZero。
– (void)addSpeedTable {
..... tableView.frame = self.view.bounds; [self.view addSubview:tableView];
}
更重要的是,你应该通过self.tableView = tableView或者你的tableView属性将你的tableView赋给属性永远不会是有效的状态。
您可以在第三行添加断点,并打印出框架,看它的边界是否为0。
我想你可能会声明错误的语法。 我无法上传图片,所以注意下面的代码的区别。 大写可能是问题。 {//服务条款static NSString * cellId = @“justCommon”;
static NSString *CellIdentifier = @"newCell"; }