我在https://stackoverflow.com/search?q=UITableView+dataSource+must+return+a+cell+from+tableView%3AcellForRowAtIndexPath进行了搜索,但我知道我需要检查单元格是否为零,但我的单元格是自定义的,我不使用 xib,我使用故事板。我也在 storyboard.as http://i.stack.imgur.com/g0rRO.png上选择类 CustomPlaceTableViewCell
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if ([cell isKindOfClass:[CustomPlaceTableViewCell class]]) { cell = (CustomPlaceTableViewCell *)cell; }
//retrieve the place info
Place *place = [self.placeDataController objectInPlaceListAtIndex:indexPath.row];
//set the cell with place info
if (place.name)
{
cell.nameLabel.text =place.name;
}
else
{
cell.nameLabel.text = @"PortAura";
}
if (place.distance)
{
cell.distanceLabel.text = place.distance;
}
else
{
cell.distanceLabel.text = @"PortAura";
}
return cell;
}
CustomPlaceTableViewCell.m
@interface CustomPlaceTableViewCell : UITableViewCell{
UILabel *nameLabel;
UILabel *distanceLabel;
UILabel *addressLabel;
}
@property (nonatomic) IBOutlet UILabel *nameLabel;
@property (nonatomic) IBOutlet UILabel *distanceLabel;
@property (nonatomic) IBOutlet UILabel *addressLabel;
@end
@implementation CustomPlaceTableViewCell
@synthesize distanceLabel,nameLabel,addressLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
当我运行我的应用程序时,它给了我:
2012-07-02 17:16:26.675 MyAura[2595:707] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格:”