尝试从UITableView中删除行后应用程序崩溃

时间:2014-04-14 05:44:15

标签: ios iphone objective-c uitableview

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.effectsTableView beginUpdates];

        Effect *effectToBeDeleted =self.effectsArray[indexPath.row];
        [self deleteEffectWithName:effectToBeDeleted.name];

        [self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
        [self.effectsArray removeObjectAtIndex:indexPath.row];

        [self.effectsTableView endUpdates];
    }
}

理论上,上面的函数应该在滑动时删除行并按下删除按钮。但是,即使在

之后,行也不会删除

[self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];

被调用。在用户滚动到UITableView的底部,在加载最后一行时,应用程序显然因{1}}错误而崩溃,因为其中一个对象已从数组中删除。

cellForRowAtIndexPath如下:

"*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 8]"

以下是整个.m文件以获得更好的上下文:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.effectsArray)
    {
        [self loadEffectsInArray];
    }

    static NSString *cellIdentifier = @"EffectsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }


    Effect *effectCellEffect = [self.effectsArray objectAtIndex:indexPath.row];
    NSString *effectCellText = effectCellEffect.name;
    [cell.textLabel setText:effectCellText];
    cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];

    return cell;
}

我在这里做错了什么?

5 个答案:

答案 0 :(得分:3)

您在哪里开始更新,请添加

[self.effectsTableView beginUpdates];

删除行之前,在编写开始更新和结束更新时无需重新加载表。

在删除行之前,请不要从数组中删除对象。

答案 1 :(得分:2)

它在工作 在commitEditingStyle方法结束时添加此行,这将有助于您

[TableViewForFirstView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

答案 2 :(得分:0)

尝试此代码在删除名为

之前添加beginUpdates方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
      [self.effectsTableView beginUpdates];
        Effect *effectToBeDeleted =self.effectsArray[indexPath.row];
        [self deleteEffectWithName:effectToBeDeleted.name];

        [self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];

        [self.effectsArray removeObjectAtIndex:indexPath.row];
        [self loadEffectsInArray];
        [self.effectsTableView endUpdates];

    }

答案 3 :(得分:0)

试试这段代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {

    Effect *effectToBeDeleted =[self.effectsArray objectAtIndex:indexPath.row];
    [self deleteEffectWithName:effectToBeDeleted.name];
    [self.effectsArray removeObjectAtIndex:indexPath.row];
    [self loadEffectsInArray];
    [self.effectsTableView reloadData];

}

答案 4 :(得分:0)

正如此消息一样,它因为超出原因而崩溃,因此请在分配之前检查数组索引:

Effect *effectToBeDeleted = self.effectsArray > indexPath.row ? self.effectsArray[indexPath.row] : nil;

if(self.effectsArray.count > indexpath.row)
 [self.effectsArray removeObjectAtIndex:indexPath.row];
else
 NSlog("out of bound");