UITableView dataSource必须从tableView:cellForRowAtIndexPath eror返回一个单元格

时间:2013-11-04 22:18:51

标签: ios ios6 uitableview

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {

    static NSString *cellId = @"Cell";
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    CurrentCell *cell = (CurrentCell *)[self.tableView dequeueReusableCellWithIdentifier:cellId];


    if (cell == nil)
    {
        NSLog(@"cell == nil");
        cell = [[CurrentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }


    NSLog(@"the cell is %@", cell);
    Current *current = [currents objectAtIndex:indexPath.section];

    [cell setCellWithCurrent:current];


    return cell;

}

我在storyboard中添加了重用标识符,设置了委托和数据源,但仍然出现了这个错误:

    *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],          /SourceCache/UIKit/UIKit-2372/UITableView.m:5471
2013-11-04 23:47:34.206 MyApp[7417:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x394452a3 0x3333497f 0x3944515d 0x342082af 0x32773f79 0x325c50a1 0x325c5055 0x325c255d 0x325a730b 0x325be7c7 0x3257a803 0x35e4ed63 0x35e4e901 0x35e4f835 0x35e4f21b 0x35e4f029 0x325808eb 0x3941a6cd 0x394189c1 0x39418d17 0x3938bebd 0x3938bd49 0x377592eb 0x325cb2f9 0xec869 0x36bf7b20)
libc++abi.dylib: terminate called throwing an exception
(lldb)

我如何用表格显示我的观点:

    else if ([segue.identifier isEqualToString:@"showCurrents"]) {

    MainViewController *mainViewController = (MainViewController*)segue.destinationViewController;

}

CurrentCell.m:

    #import "CurrentCell.h"


@interface CurrentCell ()

@end

@implementation CurrentCell
{
    UIColor *badgeColor;
    NSString *badgeText;
}

@synthesize topTitleLabel;
@synthesize descriptionLabel;

- (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
}


-(void) setCellWithCurrent:(Current*) current
{
    NSString *title = [current title];
    [self.topTitleLabel setText:title];
    [self.descriptionLabel setText:[current description]];


}

@end

PS:我没有立即得到这个错误,但是经过一段时间的快速滚动列表。

2 个答案:

答案 0 :(得分:1)

使用dequeueReusableCellWithIdentifier:forIndexPath:而不是dequeueReusableCellWithIdentifier:

删除关于检查单元格是否为零的业务。

由于您的单元格是UITableViewCell的自定义子类,因此请确保将单元格的Class设置为IB中的自定义子类(请参阅Identity Inspector)。

答案 1 :(得分:0)

尝试使用NSBundle的loadNibNamed方法加载单元格。

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Your nib file name for CurrentCell" owner:nil options:nil] objectAtIndex:0];
}