删除表格单元格导致崩溃 [英] Deleting table cell causes crash
问题描述
当使用 deleteRowsAtIndexPaths 删除表格单元格时,如果我尝试使用滑动手势删除单元格以进入编辑模式,然后点击删除,我会崩溃.如果我通过切换编辑/完成按钮进入编辑模式,然后删除单元格,表格会正常更新而不会出现问题.
When deleting table cell using deleteRowsAtIndexPaths I get a crash if I attempt the delete the cell using a swipe gesture to enter editing mode and then hit delete. If I enter editing mode by toggling an edit/done button then delete cell the table gracefully updates without a problem.
我也尝试过使用 [table beginUpdates]
&[table endUpdates]
也没有成功.[table reloadData]
也适用于任何一种情况同样如您所见,我已经尝试过 [table reloadSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation: UITableViewRowAnimationFade];
在使用编辑/完成按钮进入编辑模式时发生相同的崩溃.
I've also tried using [table beginUpdates]
& [table endUpdates]
also without success.
Also [table reloadData]
works in either case
Also as you can see I've tried [table reloadSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation: UITableViewRowAnimationFade];
with the same crash when entering editing mode with edit/done button.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSMutableArray *temp = nil;
switch (indexPath.section)
{
case 0:
{
temp = self.entireSavedArray;
break;
}
case 1:
{
temp = self.entireSavedArray2014;
break;
}
case 2:
{
temp = self.entireSavedArray2013;
break;
}
case 3:
{
temp = self.entireSavedArray2012;
break;
}
case 4:
{
temp = self.entireSavedArray2011;
break;
}
case 5:
{
temp = self.entireSavedArray2010;
break;
}
default:
break;
}
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[temp removeObjectAtIndex: ((int)indexPath.row * 11)];
[self.table deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil] withRowAnimation: UITableViewRowAnimationFade];
//[tableView reloadSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation: UITableViewRowAnimationFade];
}
}
推荐答案
问题在于滑动手势无需点击编辑/完成按钮即可将表格置于编辑模式.这通常不会有问题,但是当表格未处于编辑模式时,我将占位符单元格添加到表格中,然后在按下完成按钮时将它们添加回来.(我更改了我的数据模型)当我最初创建应用程序时,我通过在点击编辑/完成按钮时进行调整来解决这个问题.
The issue was the fact that the swiping gesture puts the table into edit mode without tapping the edit/done button. This normally would not be a problem, however when the table was not in edit mode I added place holder cells into the table, then adding them back when the done button was pressed. (I changed my data model) When I originally created the app I had solved that by making the adjustments when the edit/done button was tapped.
解决方案:由于滑动手势进入编辑模式而不点击编辑/完成按钮,我需要以另一种方式进行调整.我通过使用:
Solution: Since the swiping gesture enters edit mode without tapping the edit/done button I needed to make the adjustments in another way. I did this by using:
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self makeAdjustments];
}
并在此处进行调整:
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self makeAdjustments];
}
这篇关于删除表格单元格导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!