在普通视图控制器内的表视图

我正在做这个iPhone项目,我需要在普通视图控制器中有一个(动态)表视图。 我没有选择Table View Controller,因为我需要在页面中放入其他东西。 想象一个带有文本字段,按钮和大约4-5个单元格的小表视图的页面。

当我运行应用程序时,我需要触摸一个按钮以切换到此视图。 我点击按钮,应用程序崩溃并告诉我:

2012-07-22 14:40:57.304有多少?[3055:f803] *断言失败 – [UITableView _createPreparedCellForGlobalRow:withIndexPath:],/ SourceCache / UIKit_Sim / UIKit-1914.84 / UITableView.m:6061

这是我的.H文件:

#import  @interface ProjectViewController : UIViewController  @end 

这是我的.M文件:

 #import "ProjectViewController.h" @interface ProjectViewController () @end @implementation ProjectViewController //MyViewController.m #pragma mark - Table View Data Source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSLog(@"numberOfSectionsInTableView"); return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"numberOfRowsInSection"); return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; NSLog(@"cellForRowAtIndexPath"); cell.textLabel.text = @"Test"; return cell; } @end 

我控制 – 从表视图拖动到我的控制器来设置委托和数据源。

我做错了什么??

谢谢你的帮助。

尝试在.h文件中创建一个sockets,并将其连接到storyboardtableView

ProjectViewController.h

 @property (nonatomic, strong) IBOutlet UITableView *myTableView; 

ProjectViewController.m

 @synthesize myTableView; ... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"cell"]; NSLog(@"cellForRowAtIndexPath"); cell.textLabel.text = @"Test"; return cell; 

}

在你的-cellForRowAtIndexPath中,如果你这样做,你会没事的。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } NSLog(@"cellForRowAtIndexPath"); cell.textLabel.text = @"Test"; return cell; 

}

问题是你的单元从未分配和初始化内存。 这就是你得到错误的原因。