xcode 4.5 iOS Storyboard和UITableView代表混淆
我试图做一个简单的iOS应用程序,它使用UINavigationController两个ViewController和一个UITableView来显示一个列表。 问题是该表拒绝显示来自NSArray的数据。 所有代表都按照您在图片中看到的方式进行设置。


每一件事情,如果我为ViewController创建一个xib文件,但我不需要一个更多的xib文件,如果我可以在Storyboard中再拖一个ViewController,并将它的类链接到我的ItemListViewController,就像你在第一张图片中看到的一样。
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
如果有人能指引我正确的方向,我做错了什么。
在numberOfSectionsInTableView您返回0,表示没有要生成的部分(因此也没有行)。 你可能想要返回1。
此外,你想确保你保留你的NSMutableArray ,所以你真的想做:
@property (nonatomic, retain) NSMutableArray *tblDataSource;
链接地址: http://www.djcxy.com/p/66861.html
上一篇: xcode 4.5 iOS Storyboard and UITableView delegate confusion
下一篇: Strange issue with Indexed UITableView's section index title is incorrect
