Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView在编辑时崩溃_Iphone_Objective C - Fatal编程技术网

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个节,我从不添加节。我认为问题在于添加/删除行,而不是节。