2011-01-12 53 views
0

我目前正在为iPhone编写一个简单的表视图示例,但对于我的生活我不知道为什么会出现以下异常。UITableView与核心数据insertRowsAtIndexPaths导致NSInternalInconsistencyException

工作流程如下: 我有一个UIViewController,我添加了一个UIView。这种看法,然后我增加了编程定义的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)最初,“条目”数组是空的,所以计数为0,这似乎返回一个不为空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).' 

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

+1

你确定你想,如果你是从零开始的排阵中entries.count? – joshpaul 2011-01-12 23:24:56

回答

0

好吧想出来的人。看起来,当使用设置UITableViewDelegate的自定义UIViewController时,您还需要有一个UITableViewDataSource集并将tableViews dataSource指向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; 
}