iPhone应用程序 - 无法让细胞显示阵列数据

时间:2011-07-12 16:31:58

标签: iphone xcode uitableview

我有一个简单的表格视图,我使用书中的示例构建但它不起作用..

应该从数组中获取值并将其显示在表视图中的单元格中。我已将表视图dataSource和delegate连接到文件的所有者,并在我的控制器类中包含以下代码:

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

编译时项目成功,但表格单元格中没有显示文字......

更新

#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 :(得分:0)

您缺少numberOfSections数据源方法。

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

在更改数组中的数据后,您还需要重新加载表。

[yourTableViewHere reloadData];

答案 1 :(得分:0)

从上次评论中,您从xib文件创建了tableview,但IBOutlet在哪里对应于tableview。您必须创建一个IBOutlet UITableView * tableview并将其与xib文件中的tableview连接