我正在尝试制作一个简单的 iOS 应用程序,它使用一个 UINavigationController 两个 ViewController 和一个 UITableView 来显示列表。问题是该表拒绝显示来自 NSArray 的数据。如您在图片中所见,所有委托(delegate)均已设置。
如果我为 ViewController 创建一个 xib 文件,一切都会起作用,但是如果我可以在 Storyboard 中再拖动一个 ViewController 并将它的类链接到我的 ItemListViewController,我就不需要另一个 xib 文件,如您在第一张图片中看到的那样。
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;
关于ios - xcode 4.5 iOS Storyboard 和 UITableView delegate 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794348/