编辑时,UITableView崩溃 [英] UITableView Crashing When Editing

查看:39
本文介绍了编辑时,UITableView崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下行出现错误: [self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];

* 由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:无效的更新:无效的节数.更新(1)之后在表格视图中包含的节数必须等于更新(1)之前在表格视图中包含的节数,加上或减去插入或删除的节数(已插入1,0删除).

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).

- (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];
} else {
    // delete section
}

}

更新:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

推荐答案

请确保更新后返回当前节数.这意味着您将需要一种跟踪节​​数的方法,例如在应为 NSInteger 的私有变量 currentNumberOfSections 中.每次插入后将其递增(如果要删除节,则递减).并且不要忘记将您的 TableViewController 中的 -numberOfSectionsInTableView 方法更新为以下内容:

Please make sure that you return current number of sections after update. This means you'll need a way to track number of sections, example in a private variable currentNumberOfSections that should be NSInteger. Increment it after each insert (decrement in case you're removing a section). And don't forget to update your -numberOfSectionsInTableView method in your TableViewController to something like this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return currentNumberOfSections;
}

检查是否返回了 TableViewController 的当前节数,并在其中添加了新节.

Check that you are returning the current number of sections for the TableViewController, in which you're adding the new section.

在您的情况下,将是这样的:

In your case it would be something like this:

- (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 引用的类中添加/更新:

And then in the class referenced by self.routineTableView add/update:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return currentNumberOfSections;
}

不要忘记将 NSInteger currentNumberOfSections 添加到控制器的头文件中.我也将其放在 @private 部分中,但这取决于您.在控制器的指定初始化程序中,将此变量的值设置为首选的默认段数: currentNumberOfSections = 1 .

Don't forget to add NSInteger currentNumberOfSections to controller's header file. I would also put it in @private section, but this is up to you. In the designated initializer of the controller set the value of this variable to preferred default number of sections: currentNumberOfSections = 1.

祝你好运!

这篇关于编辑时,UITableView崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆