我是iOS开发新手,我有一个UITableViewController
,我想在表中加载NSMutableArray
数据,但它会加载数据并显示一个空表。这是代码:
#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
答案 0 :(得分:1)
Le Sigh 您会惊讶地发现,通过“让您的班级成为委托和数据源”来解决许多UITableView问题。在IB中,将出口拖到文件所有者。在代码中,设置tableView.delegate = self;和tableView.datasource = self;
答案 1 :(得分:1)
在[self.view reloadData];
实施结束时添加viewDidLoad
。
如果这没有帮助,我怀疑你的viewController实际上不是PhotoTableViewController
类型。确保正确设置。如果通过InterfaceBuilder完成,请检查viewController的类型并确保它显示为PhotoTableViewController
。