我正在尝试创建一个简单的iOS应用程序,它使用UINavigationController两个ViewController和一个UITableView来显示列表。问题是该表拒绝显示来自NSArray的数据。所有代表都按照您在图片中的设置进行设置。
如果我为ViewController创建一个xib文件,那么每件事情都有效,但如果我可以在Storyboard中再拖一个ViewController并将它的类链接到我的ItemListViewController,就像你在第一张图片中看到的那样,我不再需要一个xib文件。 / p>
ItemListViewController.h
#import @interface ItemListViewController : UIViewController{ NSMutableArray *tblDataSource; IBOutlet UITableView *tblSimpleTable; } @property (assign) NSMutableArray *tblDataSource; @property (retain) IBOutlet UITableView *tblSimpleTable; @end
ItemListViewController.m
#import "ItemListViewController.h" @interface ItemListViewController () @end @implementation ItemListViewController @synthesize tblDataSource,tblSimpleTable; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg.png"]]; self.tblSimpleTable.backgroundColor = background; [background release]; NSArray * data = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil]; [tblDataSource setArray:data]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSLog(@"@%d",[tblDataSource count]); return [tblDataSource count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... UIImage *placeholder = [UIImage imageNamed:@"icon.png"]; [cell.imageView setImage:placeholder]; cell.textLabel.text = [tblDataSource objectAtIndex:indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Clicked on row @%d",indexPath.row); } @end
如果有人能指出我正确的方向,我做错了什么。
答案 0 :(得分:2)
在numberOfSectionsInTableView
中,您返回0,表示没有要生成的部分(因此也没有行)。你可能想要返回1。
此外,您要确保保留NSMutableArray
,这样您真的想要这样做:
@property (nonatomic, retain) NSMutableArray *tblDataSource;