Ios UITableView作为子视图始终为空

Ios UITableView作为子视图始终为空,ios,objective-c,uitableview,subview,Ios,Objective C,Uitableview,Subview,我有一个ViewController,它使用函数添加了几个子视图(图) [self.view addSubview:subView] 现在,我想添加一个UITableView作为子视图,但当我这样做时,我的表总是空的。从未调用NumberOfSectionsTableView、numberOfRowsInSection等方法。 我的表没有XIB文件。这是my.M文件中的代码: #import "TableSubviewViewController.h" @implementation Tab

我有一个ViewController,它使用函数添加了几个子视图(图)

[self.view addSubview:subView]
现在,我想添加一个UITableView作为子视图,但当我这样做时,我的表总是空的。从未调用NumberOfSectionsTableView、numberOfRowsInSection等方法。 我的表没有XIB文件。这是my.M文件中的代码:

#import "TableSubviewViewController.h"

@implementation TableSubviewViewController

@synthesize data;
@synthesize tableFrame;

-(id)initWithStyle:(UITableViewStyle)style{
   self = [super initWithStyle:style];
   if (self) {
       // Custom initialization
   }
   return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.frame = tableFrame;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[self.data allKeys]count];
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[self.data allKeys]objectAtIndex:section];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1; 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"Some text";   
    return cell;
}
这是我的主ViewController中的代码,我在其中创建表SubViewController:

TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.data = data;
[self.view addObject:tableSubView.view];
我错过了什么?

谢谢

您需要这样设置数据源和委托方法

//在tableSubViewController.h文件中放置一个自定义初始化器
@接口表SubViewController:UIViewController
-(id)使用frame:(CGRect)rect初始化子视图表视图//您的自定义初始化器
@结束
//TableSubviewViewController.m文件
#导入“TableSubViewController.h”
@接口表SubViewController()
@结束
@实现表SubViewController
//不要使用此方法留空
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
//您可以自定义initiliser方法
-(id)使用frame:(CGRect)rect初始化子视图表视图
{
self=[super init];
如果(自我)
{
UITableView*subViewTable=[[UITableView alloc]initWithFrame:rect];//由于您是以编程方式创建tableview,因此需要以编程方式设置数据源和委托
subViewTable.dataSource=self;//添加此
subViewTable.delegate=self;//这也是
[self.view addSubview:subViewTable];
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
//数据源和委托方法
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回2;
}
//按要求使用
//-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
//返回3;
//}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=@“一些文本”;
返回单元;
}
//在主视图controller.m controller中,您需要这样做
TableSubViewController*subViewTable=[[TableSubViewController alloc]初始化SubViewTableViewWithFrame:CGRectMake(self.view.bounds.origin.x,self.view.bounds.origin.y,self.view.bounds.size.width,150)]//创建了子视图tableview tableview
//设置您为tableview委托和数据源定义的属性
[self.view addSubview:subViewTable.view];


希望这对你有帮助:)

你需要像这样设置数据源和委托方法

//在tableSubViewController.h文件中放置一个自定义初始化器
@接口表SubViewController:UIViewController
-(id)使用frame:(CGRect)rect初始化子视图表视图//您的自定义初始化器
@结束
//TableSubviewViewController.m文件
#导入“TableSubViewController.h”
@接口表SubViewController()
@结束
@实现表SubViewController
//不要使用此方法留空
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
//您可以自定义initiliser方法
-(id)使用frame:(CGRect)rect初始化子视图表视图
{
self=[super init];
如果(自我)
{
UITableView*subViewTable=[[UITableView alloc]initWithFrame:rect];//由于您是以编程方式创建tableview,因此需要以编程方式设置数据源和委托
subViewTable.dataSource=self;//添加此
subViewTable.delegate=self;//这也是
[self.view addSubview:subViewTable];
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
//数据源和委托方法
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回2;
}
//按要求使用
//-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
//返回3;
//}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=@“一些文本”;
返回单元;
}
//在主视图controller.m controller中,您需要这样做
   // in TableSubviewViewController.h file put a custom initilizer 

   @interface TableSubviewViewController : UIViewController
   - (id)initTheSubviewTableVIewWithFrame:(CGRect)rect; //your custom initialiser
   @end

  //TableSubviewViewController.m file 
  #import "TableSubviewViewController.h"

  @interface TableSubviewViewController ()<UITableViewDataSource,UITableViewDelegate>

  @end

  @implementation TableSubviewViewController

   //dont use this method left blank
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
    // Custom initialisation
      }
      return self;
 }
    //you can make custom initiliser method
   - (id)initTheSubviewTableVIewWithFrame:(CGRect)rect
      {
         self = [super init];
         if(self)
        {
           UITableView *subViewTable = [[UITableView alloc]initWithFrame:rect];//since u are creating the tableview programatically u need to set datasource and delegate programatically
            subViewTable.dataSource = self; //add this
            subViewTable.delegate = self; //this also
            [self.view addSubview:subViewTable];
         }

          return self;
      }


     - (void)viewDidLoad
       {
         [super viewDidLoad];
         // Do any additional setup after loading the view from its nib.
       }

      - (void)didReceiveMemoryWarning
     {
         [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
      }

     //datasource and delegate methods
      -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
       return 2;
       }

      //use as reqired
      //-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:  (NSInteger)section{
      //    return 3;
      //}

      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
       }

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell == nil){
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    cell.textLabel.text = @"Some text";
    return cell;
   }

      //in  main ViewController.m controler u need to do like this
      TableSubviewViewController *subViewTable = [[TableSubviewViewController  alloc]initTheSubviewTableVIewWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 150)];//created the subviewTableView tableview 
     //set properties that u defined for tableview delegate and datasource



     [self.view addSubview:subViewTable.view];
TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.delegate = self;
tableSubView.dataSource = self;
tableSubView.data = data;

[self.view addObject:tableSubView.view];