Tableview 在 UIViewController 上不可见
Tableview is not visible on UIViewController
UITableView
在 UIViewController Class.
上不可见
在Storyboard
上添加了一个新的UIViewController
设置到相关的SlideViewcontroller class,添加一个新的UITableView
(tbl_Slide),设置数据源并将添加的 UITableView 委托给情节提要中的 UIViewController。
并且还包括相关的数据源和委托方法到 UIViewController class.
- (void)viewDidLoad {
[super viewDidLoad];
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu];
}
既然把UITableView添加到UIView的UIViewController中,就不用再诱导上面两行代码了
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tbl_SlideMenu];
一旦我包含分配并添加到子视图,它就会用表视图显示数据。
删除分配后,添加到子视图的框架不可见。
那么故事板需要什么,我将 UITableView 添加到 UIViewController 的 UIView(默认视图)。一旦组件添加到故事板并构建,就不需要在代码中分配和构建。
提前致谢
希望得到最佳答案
如果您在情节提要中添加了表视图,则无需在代码中初始化视图组件,在这种情况下,您不需要下面提到的行
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
但是你需要为你的 tableview
创建一个 outlet
来访问 code
中的表视图,这意味着一旦加载视图,你的所有视图组件都会在你的情况下初始化tableview 已初始化,您可以准备使用它,如果您在情节提要中设置数据源和委托,您也不需要下面的代码行
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
由于您配置了 tableview 数据源和委托,并且还添加了 tableview 以查看控制器的视图,因此也不需要下面的行
[self.view addSubview:self.tbl_SlideMenu];
第二种方式是你提到的用纯代码创建视图,在这种情况下,你必须自己做所有事情,从初始化到将视图组件添加到控制器的视图,如下所述
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; //creating the tableview
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //configuring the view
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self; //setting the datasource and delegate
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu]; //adding it to tableview
希望你能得到这个
编辑
它对我来说工作正常,我用一些虚拟值交叉检查了它,例如我提到的,拖放了一个表视图,将其数据源和委托设置为故事板中的 View Controller
并连接了一个插座在 ViewController.h
文件中,如下所示,
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tbl_SlideMenu;
@end
并在 ViewController.m
文件中
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
cell.textLabel.text = @"Hello";
return cell;
}
@end
我得到的输出显示每行有 5 个 "Hello" 的表格视图
UITableView
在 UIViewController Class.
在Storyboard
上添加了一个新的UIViewController
设置到相关的SlideViewcontroller class,添加一个新的UITableView
(tbl_Slide),设置数据源并将添加的 UITableView 委托给情节提要中的 UIViewController。
并且还包括相关的数据源和委托方法到 UIViewController class.
- (void)viewDidLoad {
[super viewDidLoad];
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu];
}
既然把UITableView添加到UIView的UIViewController中,就不用再诱导上面两行代码了
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tbl_SlideMenu];
一旦我包含分配并添加到子视图,它就会用表视图显示数据。 删除分配后,添加到子视图的框架不可见。
那么故事板需要什么,我将 UITableView 添加到 UIViewController 的 UIView(默认视图)。一旦组件添加到故事板并构建,就不需要在代码中分配和构建。
提前致谢 希望得到最佳答案
如果您在情节提要中添加了表视图,则无需在代码中初始化视图组件,在这种情况下,您不需要下面提到的行
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
但是你需要为你的 tableview
创建一个 outlet
来访问 code
中的表视图,这意味着一旦加载视图,你的所有视图组件都会在你的情况下初始化tableview 已初始化,您可以准备使用它,如果您在情节提要中设置数据源和委托,您也不需要下面的代码行
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
由于您配置了 tableview 数据源和委托,并且还添加了 tableview 以查看控制器的视图,因此也不需要下面的行
[self.view addSubview:self.tbl_SlideMenu];
第二种方式是你提到的用纯代码创建视图,在这种情况下,你必须自己做所有事情,从初始化到将视图组件添加到控制器的视图,如下所述
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; //creating the tableview
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //configuring the view
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self; //setting the datasource and delegate
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu]; //adding it to tableview
希望你能得到这个
编辑
它对我来说工作正常,我用一些虚拟值交叉检查了它,例如我提到的,拖放了一个表视图,将其数据源和委托设置为故事板中的 View Controller
并连接了一个插座在 ViewController.h
文件中,如下所示,
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tbl_SlideMenu;
@end
并在 ViewController.m
文件中
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
cell.textLabel.text = @"Hello";
return cell;
}
@end
我得到的输出显示每行有 5 个 "Hello" 的表格视图