Iphone UITableView数据源必须从tableView返回一个单元格:cellForRowAtIndexPath
我在搜索,但我知道我需要检查单元格是否为零,但我的单元格是自定义的,我没有使用xib,我使用的是storyboard。我还在storyboard.as中选择CustomPlaceTableViewCell类 代码:Iphone UITableView数据源必须从tableView返回一个单元格:cellForRowAtIndexPath,iphone,ios5,Iphone,Ios5,我在搜索,但我知道我需要检查单元格是否为零,但我的单元格是自定义的,我没有使用xib,我使用的是storyboard。我还在storyboard.as中选择CustomPlaceTableViewCell类 代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomPlaceTableViewCell *cell = [tab
- (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]***由于未捕获的异常“NSInternalinconsistenceException”而终止应用程序,原因:“UITableView数据源必须从tableView返回单元格:cellForRowAtIndexPath:”
返回单元格代码>在结尾处缺失我想您错过了tableView中的返回单元格:cellforrowatinexpath
试试这个
ans检查或使用此NSLog(@“description=%@,[cell description])代码>
如果[cell description]
为空,则将其更改
CustomPlaceTableViewCell *cell = (CustomPlaceTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
是的,即使您使用了上述所有建议,您的代码也会崩溃。您是否检查过您的单元格是否正在实际创建
不,这不是创建的,此方法实际上返回一个nil
值
在您的代码中使用下面的行是试图使用可重用单元,但若并没有创建单元呢
CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
您必须在编写代码后检查此项,如:
if (cell == nil) {
cell = [[CustomPlaceTableViewCell alloc] init]; // or your custom initialization
}
在此之后,您的手机将不会为零,应用程序也不会崩溃
希望这能解决您的问题首先我使用:
PlaceMasterViewController *placeController = [[PlaceMasterViewController alloc]init];
[self.navigationController pushViewController:placeController animated:YES];
因为表中没有数据,或者给我错误,所以我使用:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
PlaceMasterViewController *placeController = (PlaceMasterViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"peoplemastViewControl"];
[self.navigationController pushViewController:placeController animated:YES];
一切正常我也面临同样的问题,但我解决了
我面临这个问题是因为,我在单独的Xib文件中创建了talbeViewCell,并将tableview类作为文件的所有者。但是,我还没有将我的手机IBOutlet链接到我的tableview类
因此,将我的tableviewcell Xib链接到在tableview中创建的IBOutlet,我解决了这个问题
您将遇到此问题,因为您在CellForRowatinex中装箱的tableviewcell为零 我们没有收到您的问题,请明确说明我们可以为您做些什么。您可以在您的CellForRowatineXpath中发布完整的代码吗?当然,您应该将其返回到return cell;我已添加返回单元格,不可用CustomPlaceTableViewCell.xib将类名从UITableViewCell更改为CustomPlaceTableViewCell我未使用xib,我使用的是情节提要,我已修改CustomPlaceTableViewCell选项。请检查您的单元格地址,您的单元格地址为零,再次使用此NSLog(@“description=%@,[cell description]);让我们通过我的应用程序不要崩溃,但视图中没有任何数据显示,它给我一个无数据表您指定的由于未捕获的异常而终止应用程序'nsinternalinconsistenceexception'
,这就是我相应回答的原因。是的,但如果我不使用自定义单元格,我使用的是系统单元格,一切都可以,但我的标签不仅仅是系统cell@KapilChoubisa因为他使用的是情节提要,所以不需要检查单元格是否被创建。并且只要确保CellIdentifier名称与使用序列图像板创建tableview时在属性中提供的名称相同即可。是的,CellIdentifier与使用序列图像板创建tableview时的属性相同。
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
PlaceMasterViewController *placeController = (PlaceMasterViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"peoplemastViewControl"];
[self.navigationController pushViewController:placeController animated:YES];