iphone - UITableViewController 不加载数据

标签 iphone objective-c ios ios5

我是 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

最佳答案

viewDidLoad 实现的末尾添加 [self.view reloadData];

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

enter image description here

关于iphone - UITableViewController 不加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147847/

相关文章:

php - iOS、PHP 和 mySQL。在 iPhone 上显示数据库中的数据

ios - 屏幕顶部边缘的自定义拖动 View 与通知中心冲突

ios - 如何在耳机的两个芽之间切换音频输出

ios - 当我的整个应用程序锁定在纵向模式时,以横向模式全屏播放视频

iphone - applicationwillterminate 内的回调

ios - 请求失败 : unacceptable content-type: text/plain using AFNetworking 2. 0

ios - 在 SceneKit (Swift) 中点击/选择节点

android - unity3d 中的能源效率 - 暂停应用程序等待用户交互

使用 wifi 进行 iPhone 接近检测

iphone - 一次IOS内购可以恢复多少台设备?