2012-08-19 86 views
0

可能重复:
UITableViewController not loading data的UITableViewController没有显示数据

我在下面的方法创建一个UITableViewController

#import "KLActionsViewController.h" 

@interface KLActionsViewController() 

@end 

@implementation KLActionsViewController 
@synthesize actionList,delegate; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)loadView 
{ 

} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSMutableArray* list = [[NSMutableArray alloc] init]; 
    self.tableView = [[UITableView alloc] init]; 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    self.actionList = list; 
    self.clearsSelectionOnViewWillAppear = NO; 
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 400.0); 
    [self.actionList addObject:@"Obj1"]; 
    [self.actionList addObject:@"Obj2"]; 
    [self.actionList addObject:@"Obj3"]; 
    [self.actionList addObject:@"Obj4"]; 

} 

- (int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"in number of section"); 
    return 1; 
} 

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    NSString* lab = [self.actionList objectAtIndex:indexPath.row]; 
// NSLog(lab); 
    NSLog(@"here in there"); 
    cell.textLabel.text = lab; 
    return cell; 
} 

- (NSInteger)numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"Total entries : %i ",[self.actionList count]); 
    return [self.actionList count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.delegate != nil) { 
     [self.delegate actionSelected:indexPath.row]; 
    } 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.actionList = nil; 
    self.delegate = nil ; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

但是当我尝试创建它的实例和在UIPopoverController中显示它,我能看到的只有一张空表,并且没有数据。此外,numberOfRowsInSectioncellForRowAtIndexPath永远不会被调用。 有什么帮助吗?

+0

尝试删除该loadView“实施” – mijokaliger 2012-08-19 15:15:43

+0

试过,仍然没有帮助 – vishalv2050 2012-08-20 05:18:09

回答

0

您正在viewDidLoad:内创建一个新的表格视图,但不会将其作为此控制器视图的子视图。既然你说你看到一个空表,这就让我相信你正从你的.xib文件中加载一个表,但只设置这个新表的dataSource

如果这是它的结构,请使用表视图中的.xib连接到self.tableView属性,并且不要创建新对象...只需配置您拥有的对象。

+0

这是一个'UITableViewController',我为已经是它的一部分的tableView分配内存。我是否仍然需要通过执行类似'[self.view addSubview:self.tableView]'的子视图来添加它?另外我没有任何.xib文件。我只想显示这张表。 – vishalv2050 2012-08-19 15:57:43

+0

由于您显示'initWithNibName:'它建议**正在使用**。什么代码看起来像创建一个'KLActionsViewController'?如果您在代码中而不是在.xib中创建视图层次结构,请查看'loadView'方法的文档,尤其是它讨论更新'view'属性的部分。 – 2012-08-19 16:23:59

+0

这是我正在做的加载它 'vc = [[KLActionsViewController alloc] init]; initWithContentViewController:vc]; actionsController = [[UIPopoverController alloc] initWithContentViewController:vc]; ' – vishalv2050 2012-08-19 16:38:41