ios - Tableview 在 UIViewController 上不可见

标签 ios objective-c uitableview uiviewcontroller storyboard

UITableView 在 UIViewController 类中不可见。

Storyboard 上添加了一个新的 UIViewController 设置相关的 SlideViewcontroller 类,添加一个新的 UITableView (tbl_Slide),设置数据源和将添加的 UITableView 委托(delegate)给 Storyboard 中的 UIViewController。 并且还在 UIViewController 类中包含了相关的数据源和委托(delegate)方法。

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

既然UIView的UIViewController中添加了UITableView,就不用再诱导上面两行代码了

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

一旦我包含分配并添加到 subview ,它就会用 TableView 显示数据。 删除分配后,添加到 subview 的框架将不可见。

那么 Storyboard需要什么,我将 UITableView 添加到 UIViewController 以获得 UIView(默认 View )。一旦组件添加到 Storyboard并构建,就不需要在代码中分配和构建。

提前致谢 希望得到最好的答案

最佳答案

如果您在 Storyboard 中添加了 TableView ,则无需在代码中初始化 View 组件,在这种情况下您不需要下面提到的几行

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,这意味着一旦 View 被加载你所有的在您的案例中 View 组件已初始化 TableView 已初始化并且您可以准备使用它并且如果您在 Storyboard 中设置数据源和委托(delegate)则您也不需要下面的代码行

self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self; 

因为你配置了你的 tableview 数据源和委托(delegate),并且还添加了 tableview 来查看 Controller 的 View ,所以也不需要下面的行

[self.view addSubview:self.tbl_SlideMenu];

第二种方式是你提到的用纯代码创建 View ,在这种情况下,你必须自己做所有事情,从初始化到将 View 组件添加到 Controller 的 View 中,就像你在下面提到的那样

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

希望你明白这一点

编辑

它对我来说工作正常,我用一些虚拟值交叉检查它,例如我提到的,拖放一个 TableView ,将其数据源和委托(delegate)设置为 Storyboard中的 View Controller 并连接ViewController.h 文件中的 socket ,如下所示,

#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”的 tableview

关于ios - Tableview 在 UIViewController 上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33430954/

相关文章:

android - 如何从我的 C# 代码中通过自定义 url 方案访问传递到我的应用程序中的值?

ios - Peek-and-Pop UIView 显示 SFSafariViewController

ios - 如何从现有的 Xcode 项目中删除 Apple Watch 应用程序?

ios - 无法使 UILabel 文本发光

ios - 滚动到顶部,显示估计的单元高度

iphone - 将 SQL 行加载到索引的 UITableView

iOS 到 Node.js 后端 SSL 握手失败

ios - 无法使用 AFNetworking 2.0 使用 GET 方法获取响应

iphone - quartz 2D 笔划对齐

iphone - 无法在横向模式下增加 UILabel 的宽度