1

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

[self.view addSubview:subView]

现在,我想添加一个 UITableView 作为子视图,但是当我这样做时,我的表总是空的。numberOfSectionsInTableView、numberOfRowsInSection 等方法永远不会被调用。我的表没有 XIB 文件。这是我的 .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 中的代码,我正在其中创建 TableSubviewViewController:

TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.data = data;
[self.view addObject:tableSubView.view];

我错过了什么?
谢谢

4

3 回答 3

1

TableSubviewViewController tableSubView必须是主ViewController的全局变量,不能是主ViewController的viewDidLoad方法中的局部变量

于 2014-09-18T08:34:04.390 回答
0

确保您的 .h 文件使用

并将委托和数据源设置为self。

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

[self.view addObject:tableSubView.view];

之后检查数组 self.data 是否有对象。数组可能是空白的。

于 2014-09-18T09:03:03.213 回答
0

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

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


希望这可以帮助你:)

于 2013-09-20T05:16:17.703 回答