Iphone UITableView在编辑时崩溃
在以下行获取错误:Iphone UITableView在编辑时崩溃,iphone,objective-c,Iphone,Objective C,在以下行获取错误:[self.routineTableView insertSections:[NSIndexSet indexetwithindex:1]with rowanimation:uitableview rowanimationbottom] *由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“更新无效:节数无效。”。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(1),加上或减去插入或删除的节数(1插入,0删除
[self.routineTableView insertSections:[NSIndexSet indexetwithindex:1]with rowanimation:uitableview rowanimationbottom]代码>
*由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“更新无效:节数无效。”。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(1),加上或减去插入或删除的节数(1插入,0删除)
}
更新:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
请确保更新后返回当前节数。这意味着您需要一种跟踪节数的方法,例如在私有变量currentNumberOfSections
中,该变量应为NSInteger
。每次插入后递增(在删除节时递减)。不要忘记将TableViewController
中的-numberOfSectionsInTableView
方法更新为如下内容:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return currentNumberOfSections;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.routineTableView setEditing:editing animated:animated];
if(editing){
[self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
currentNumberOfSections++;
} else {
// delete section
}
检查是否返回要添加新节的TableViewController
的当前节数
在您的情况下,可能是这样的:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return currentNumberOfSections;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.routineTableView setEditing:editing animated:animated];
if(editing){
[self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
currentNumberOfSections++;
} else {
// delete section
}
然后在self.routineTableView
add/update引用的类中:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return currentNumberOfSections;
}
不要忘记将NSInteger currentNumberOfSections
添加到控制器的头文件中。我还想把它放在@private
部分,但这取决于您。在控制器的指定初始值设定项中,将此变量的值设置为首选默认节数:currentNumberOfSections=1
祝你好运 谢谢齐吉。我明白你的意思,但我仍然不知道我该怎么做才能解决这个问题。我刚刚向问题添加了numberOfSections
方法。可能问题是您正在将一个节插入到self.routineTableView
中,并从其他控制器返回节数self.fetchedResultsController
。谢谢,但如果我替换返回[[self.fetchedResultsController节数];代码>带有返回currentNumberOfSections代码>,这在最初加载时不会导致问题吗?这一点很好。将currentNumberOfSections=1
添加到初始值设定项。如果1
是此表中的默认节数。问题是,该表应始终有1个节,我从不添加节。我认为问题在于添加/删除行,而不是节。