UITableViewController不显示数据

时间:2012-08-19 14:54:57

标签: ios uitableview

  

可能重复:
  UITableViewController not loading data

我已按以下方式创建了一个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永远不会被调用。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

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

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