iPhone App - проблема с получением ячеек для отображения данных массива - PullRequest
0 голосов
/ 12 июля 2011

У меня есть простое табличное представление, которое я построил на примере из книги, но оно не работает ..

Предполагается взять значения из массива и отобразить их в ячейках в табличном представлении. Я подключил таблицу dataSource и делегат к владельцу файла, и у меня в контроллере есть следующий код:

Simple_TableViewController.h

@interface Simple_TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *listData;
}
@property (nonatomic, retain) NSArray *listData;
@end

Simple_TableViewController.m

#import "Simple_TableViewController.h"

@implementation Simple_TableViewController
@synthesize listData;

-(void)viewdidLoad{
    NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy", @"Bashful", @"Happy", @"Doc", @"Grumpy", @"Dopey", @"Thorin",@"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin", @"Gloin", @"Bifur", @"Bofur", @"Bombur", nil];
    self.listData = array;
    [array release];
    [super viewDidLoad];
}

- (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.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.listData = nil;
    [super viewDidUnload];
}


- (void)dealloc {
    [listData release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.listData count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 
    return cell;
}
@end

Проект завершается успешно при компиляции, но текст не отображается в ячейках таблицы ...

UPDATE

#import <UIKit/UIKit.h>

@interface Simple_TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *tv;
    NSArray *listData;
}
@property (nonatomic, retain) NSArray* listData;
**@property (nonatomic, retain) UITableView* tv;**
@end

#import "Simple_TableViewController.h"

@implementation Simple_TableViewController
@synthesize listData, tv;

-(void)viewdidLoad{
    NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy", @"Bashful", @"Happy", @"Doc", @"Grumpy", @"Dopey", @"Thorin",@"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin", @"Gloin", @"Bifur", @"Bofur", @"Bombur", nil];
    self.listData = array;
    **[tv reloadData];**
    [array release];
    [super viewDidLoad];
}

- (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.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.listData = nil;
    [super viewDidUnload];
}


- (void)dealloc {
    [listData release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSString *msg = @"message";
    NSLog(@"%@", msg);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 
    return cell;
}

@end

Ответы [ 2 ]

0 голосов
/ 12 июля 2011

Из вашего последнего комментария вы сделали табличное представление из файла xib, но где IBOutlet соответствует табличному представлению. Вы должны сделать IBOutlet UITableView * tableview и связать его с вашим tableview в xib-файле

0 голосов
/ 12 июля 2011

Вам не хватает метода источника данных numberOfSections.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

Также после изменения данных в вашем массиве вы захотите перезагрузить таблицу.

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