Iphone UITableViewController不加载数据
我是iOS开发新手,我有一个Iphone UITableViewController不加载数据,iphone,objective-c,ios,ios5,Iphone,Objective C,Ios,Ios5,我是iOS开发新手,我有一个UITableViewController,我想在表中加载NSMutableArray数据,但它确实加载数据并显示一个空表。这是代码: #import "PhotosTableViewController.h" @implementation PhotosTableViewController NSMutableArray *photos; - (id)initWithStyle:(UITableViewStyle)style { self = [sup
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
Le Sigh“让您的类成为委托和数据源”解决了多少UITableView问题,您一定会感到惊讶。在IB中,将出口拖动到文件所有者。在代码中,设置tableView.delegate=self;和tableView.datasource=self 添加
[self.view重载数据]代码>在viewDidLoad
实现的末尾
如果这还没有帮助,我怀疑您的viewController实际上不是类型PhotoTableViewController
。确保安装正确。如果通过InterfaceBuilder完成,请检查viewController的类型,并确保其显示为PhotoTableViewController
在viewDidLoad中,尝试使用self分配table.dataSource和table.delegate。如何创建该viewController?显示一些代码。因为这段代码是有效的。我从“控件”中拖拽了它。它是整个项目文件,SDK 4.2,尽管这应该已经是UITableViewController的默认值。它是SDK 4.2.1,您可以编辑我的代码吗?我不能照你说的做:(Maysam,我没有代表编辑你的帖子。你使用的是界面生成器还是代码?Matthias,我看不到.h,所以我无法判断它是UIViewControllet子类还是UITableViewController子类。无论哪种方式,都要将其设置为安全的。在SDK 4.2中,没有单独的界面生成器。它是表视图控制器编辑问题到swer无论如何都不是一个好主意。只需在viewDidLoad
方法的末尾添加self.tableView.dataSource=self;self.tableView.delegate=self;
。我想还有更大的问题,它不会运行PhotoTableViewController.m
,当我放入NSLog(@“foo”)时,它不会在调试器中显示“foo”
在didViewLoad
中,viewController是否实例化为PhotoTableViewController?如果通过XIB文件实例化,请检查界面生成器,查看该viewController是否设置为PhotoTableViewController类型。我怀疑它显示的是UITableViewController,而不是PhotoTableViewController。我使用的是故事板,让我查找它:/你救了我的后半天!呵呵,我的荣幸-喝杯茶,改变一下天气;)