2012-02-05 80 views
0

我是iOS开发新手,我有一个UITableViewController,我想在表中加载NSMutableArray数据,但它确实会加载数据并显示一个空表。这是代码:UITableViewController不加载数据

#import "PhotosTableViewController.h" 


@implementation PhotosTableViewController 
NSMutableArray *photos; 

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

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    photos = [[NSMutableArray alloc] init]; 
    Photo *pic = [[Photo alloc] init]; 
    [pic setName:@"over look"]; 
    [pic setFilename:@"overlook.png"]; 
    [pic setNotes:@"this is notw!"]; 
    [photos addObject:pic]; 

    pic = [[Photo alloc] init]; 
    [pic setName:@"olives"]; 
    [pic setFilename:@"olives.png"]; 
    [pic setNotes:@"this is notw!"]; 
    [photos addObject:pic]; 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [photos count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    Photo *current=[photos objectAtIndex:indexPath.row]; 
    cell.textLabel.text=[current name]; 
    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 

} 

@end 
+1

在viewDidLoad中尝试分配table.dataSource和table.delegate自。 – Mrunal 2012-02-05 08:10:46

+0

你如何创建viewController?显示一些代码。因为这段代码有效。 – 2012-02-05 08:53:50

+0

我从'Controls'中拖动它。 – Maysam 2012-02-05 09:21:11

回答

1

在您的viewDidLoad实施结束时添加[self.view reloadData];

如果这还没有帮助,我怀疑你的viewController实际上不是PhotoTableViewController类型。确保设置正确。如果通过InterfaceBuilder完成,请检查viewController的类型并确保它说PhotoTableViewController

enter image description here

+0

我想那里是更大的问题,它不会运行'PhotoTableViewController.m',当我把'NSLog(@“foo”)'放在'didViewLoad'中时它不会在调试器中显示'foo'。 – Maysam 2012-02-05 10:58:08

+0

viewController实例化为PhotoT ableViewController?如果通过XIB文件实例化,请检查接口构建器并查看该viewController是否设置为PhotoTableViewController类型。我怀疑它说的是UITableViewController而不是PhotoTableViewController。 – Till 2012-02-05 11:05:21

+0

我正在使用故事板,让我找它:/ – Maysam 2012-02-05 11:21:13

1

乐叹你会惊奇地发现问题的UITableView有多少以“让您的课代表和数据源。”解决。在IB中,将出口拖到文件所有者。在代码中,设置tableView.delegate = self;和tableView.datasource = self;

+0

虽然这应该是UITableViewController的默认设置。 – 2012-02-05 08:19:30

+0

它是SDK 4.2.1,你能编辑我的代码吗?我不能做你说的话:( – Maysam 2012-02-05 08:20:35

+0

Maysam,我没有代表编辑你的文章,你使用的是界面生成器还是代码?而Matthias,我看不到.h,所以我可以'如果它是一个UIViewControllet子类或一个UITableViewController子类,无论哪种方式,将它设置为安全 – CodaFi 2012-02-05 08:23:01