繁体   English   中英

带有核心数据insertRowsAtIndexPaths的UITableView导致NSInternalInconsistencyException

[英]UITableView with Core Data insertRowsAtIndexPaths causes NSInternalInconsistencyException

我目前正在为iPhone编写一个简单的表格视图示例,但是为了我的生命,我无法弄清为什么发生以下异常。

工作流程如下:我有一个要添加UIView的UIViewController。 然后向该视图添加以编程方式定义的UITableView

- (id)init{
    [super initWithNibName:nil bundle:nil];

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 180, 280, 180) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor redColor];
    tableView.delegate = self;

    return self;
}

完成此操作后,我将使用UITableViewDelegate并覆盖以下方法,这将导致表被编辑。 这是由按钮和选择器方法触发的

- (void)editList:(id)sender{
    [self setEditing:YES animated:YES];
    [editButton addTarget:self action:@selector(doneList:) forControlEvents:UIControlEventTouchUpInside];
    [editButton setBackgroundImage:[UIImage imageNamed:@"ApplyChanges.png"] forState:UIControlStateNormal];
}

还有一个称为doneList的方法,该方法在完成时触发,但是代码并没有那么远。 因此,一旦单击按钮,就会调用我的setEditing代表,并引发错误。

这是委托方法

- (void)setEditing:(BOOL)flag animated:(BOOL)animated {
    NSLog(@"setEditing");
    // Always call super implementation of this method, it needs to do some work
    [super setEditing:flag animated:animated];
    // We need to insert/remove a new row in to table view to say "Add New Item..."
    if (flag) {
        // If entering edit mode, we add another row to our table view
        int count = entries.count;
        NSLog(@"int: %i", count);
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];    
    } else {
        // If leaving edit mode, we remove last row from table view
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[entries count] inSection:0];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

关于在执行过程中这段代码的几点注意事项:1)最初,“ entries”数组为空,因此计数为0,这似乎返回了一个不为null的indexPath对象2)当条目中填充了伪数据时,计数正确地增加了,但是错误仍然出现3)我尝试删除超级setEditing调用,仍然发生错误

最后是错误。

2011-01-12 16:46:13.623 Book[6256:40b] numberOfRowsInSection
2011-01-12 16:46:13.625 Book[6256:40b] numberOfRowsInSection
2011-01-12 16:46:17.658 Book[6256:40b] setEditing
2011-01-12 16:46:17.659 Book[6256:40b] int: 0
2011-01-12 16:46:17.660 Book[6256:40b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-01-12 16:46:17.692 Book[6256:40b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'

请让我知道是否缺少一些明显的东西,是否可能需要包括另一个我不知道的委托方法?

Ok弄清楚了。 看来,使用设置UITableViewDelegate的自定义UIViewController时,还需要设置UITableViewDataSource并将tableViews数据源指向self。

示例代码。

旧代码

// Header File 

@interface MyViewController : UIViewController <UITableViewDelegate> {

}

// Main File

- (id)init{
    [super initWithNibName:nil bundle:nil];

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 180, 280, 180) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor redColor];
    tableView.delegate = self;

    return self;
}

更新的工作代码

// Header File 

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

}

// Main File

- (id)init{
    [super initWithNibName:nil bundle:nil];

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 180, 280, 180) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor redColor];
    tableView.delegate = self;
    tableView.dataSource = self;

    return self;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM