UITableViewController не загружает данные - PullRequest
0 голосов
/ 05 февраля 2012

Я новичок в разработке для 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

Ответы [ 2 ]

1 голос
/ 05 февраля 2012

Добавьте [self.view reloadData]; в конце вашей viewDidLoad реализации.

Если это уже не помогает, я подозреваю, что ваш viewController на самом деле не относится к типу PhotoTableViewController. Убедитесь, что это правильно настроено. Если это сделано через InterfaceBuilder, проверьте тип viewController и убедитесь, что он говорит PhotoTableViewController.

enter image description here

1 голос
/ 05 февраля 2012

Le Sigh вы будете удивлены, как много вопросов UITableView решаются с помощью «Сделайте ваш класс делегатом и источником данных». В IB перетащите розетки в файлы владельца. В коде установите tableView.delegate = self; и tableView.datasource = self;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...