【发布时间】:2012-03-10 23:51:15
【问题描述】:
我正在尝试为我的 UITableView 创建一个自定义单元格。我正在使用 Xcode 4.2 并将情节提要与 ARC 一起使用。
我创建了一个类来表示自定义单元格,如下所示:
ResultsCustomCell.h
#import <UIKit/UIKit.h>
@interface ResultsCustomCell : UITableViewCell
{
IBOutlet UILabel *customLabel;
}
@property (nonatomic, retain) IBOutlet UILabel* customLabel;
@end
ResultsCustomCell.m
#import "ResultsCustomCell.h"
@implementation ResultsCustomCell
@synthesize customLabel;
@end
然后我在我的视图控制器中实现了 UITableView 方法,如下所示: ViewController.m
#import "ViewController.h"
#import "ResultsCustomCell.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
myCell.customLabel.text = @"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
@end
应用程序构建成功,但立即崩溃并给我以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
我错过了什么?
【问题讨论】:
-
如果您使用的是 iOS 5,请检查 stackoverflow.com/questions/12016331/…
标签: iphone objective-c xcode ios5 uitableview