0
 -(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;

}

我在情节提要、设置委托和数据源中添加了重用标识符,但仍然出现此错误:

    *** 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;

}

当前单元格.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:我没有立即收到此错误,而是在快速滚动列表一段时间后。

4

2 回答 2

1

使用 dequeueReusableCellWithIdentifier:forIndexPath: 代替 dequeueReusableCellWithIdentifier:

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

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

于 2013-11-04T22:33:11.823 回答
0

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

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Your nib file name for CurrentCell" owner:nil options:nil] objectAtIndex:0];
}
于 2013-11-04T23:33:55.337 回答