从tableview部分删除最后一行时,iOS应用程序崩溃

时间:2012-03-12 04:34:21

标签: objective-c ios uitableview

我正在学习可编辑的UITableViews编辑,我在查找删除其中一个部分中最后一行时我的应用程序崩溃的原因时遇到了麻烦。我一直在这里和谷歌一起看,它在我看来,我已经做了大多数人说的解决这个问题,但没有运气。我很难过,并感谢所提供的任何帮助。

这是我删除表格行的方法

myTableSections是一个NSMutableArray,其中包含代表各个部分的NSDictionaries - “Possessions”是包含行数组的键,以及部分标题的键“Header”

PossessionStore是一个单身人士,在allPossessions数组中保存应用程序的所有Possessions

divideSections的msg是一种将allPossessions排序为NSDictionaries并将其存储到myTableSections中的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{       ////If the table view is asking to commit the delete command
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Get the possesion from the row
        Possession *p = [[[myTableSections objectAtIndex:[indexPath section]] objectForKey:@"Possessions"] objectAtIndex:[indexPath row]];

        // delete it from the PossessionStore
        [[[PossessionStore defaultStore] allPossessions] removeObjectIdenticalTo:p];

        // Rebuild myTableViewSections to reflect the data change to the PossessionStore
        [self divideSections];

            // remove the row from the table view with an animation
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }
}

哦,是的,差点忘了这最后一部分 - 调试信息 - 此时第1部分有两行,第2部分有一行。然后从第1部分删除一行,然后从第1部分删除最后一行。我输入的NSLog报告和错误信息部分计数是正确的,因为它应该在那一点但它没有显示该部分被删除。如果我理解正确的话???????????

2012-03-12 00:40:49.938 HomePwnr[53434:f803] commitEditingStyle:forRowAtIndexPath: called
2012-03-12 00:40:49.940 HomePwnr[53434:f803] myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000",
            "Blue Gun (0X2Q7): Worth $14, recorded on 2012-03-12 04:40:40 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:40:49.942 HomePwnr[53434:f803] divideSections called
2012-03-12 00:40:49.944 HomePwnr[53434:f803]  End of divideSections myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:40:49.946 HomePwnr[53434:f803] returning number of sections: 2
2012-03-12 00:40:49.947 HomePwnr[53434:f803] returning number of sections: 2
2012-03-12 00:40:49.949 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:40:49.951 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:40:49.953 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 0, returning - 1
2012-03-12 00:40:49.954 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 1
2012-03-12 00:40:49.956 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 1
2012-03-12 00:40:49.957 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 1, returning - 1
2012-03-12 00:41:32.232 HomePwnr[53434:f803] commitEditingStyle:forRowAtIndexPath: called
2012-03-12 00:41:32.234 HomePwnr[53434:f803] myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:41:32.236 HomePwnr[53434:f803] divideSections called
2012-03-12 00:41:32.238 HomePwnr[53434:f803]  End of divideSections myTableSections is holding - (
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:41:32.239 HomePwnr[53434:f803] returning number of sections: 1
2012-03-12 00:41:32.241 HomePwnr[53434:f803] returning number of sections: 1
2012-03-12 00:41:32.242 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:41:32.244 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:41:32.246 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 0, returning - 1
2012-03-12 00:41:32.248 HomePwnr[53434:f803] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1021
2012-03-12 00:41:32.250 HomePwnr[53434:f803] *** 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 (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
*** First throw call stack:
(0x13cc022 0x155dcd6 0x1374a48 0x9ad2cb 0x9b103 0xa66d2 0xa674d 0x3409 0xb26e2 0x208e6b 0x13cde99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x12b6ef5 0x13a0195 0x1304ff2 0x13038da 0x1302d84 0x1302c9b 0x12b57d8 0x12b588a 0x16626 0x20dc 0x2085)
terminate called throwing an exception(lldb) 

2 个答案:

答案 0 :(得分:9)

所有UITableview部分必须至少有一行。

因此,当您要删除最后一行时,您必须删除该部分:

 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

确保您的数据正确处理空白部分(提供部分数量等)

答案 1 :(得分:8)

昨晚所有人都向我指出正确方向的道歉。我太累了,很沮丧得到它,我一直在打击这个太久了。这是我修复它的最终解决方案。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{       ////If the table view is asking to commit the delete command
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        BOOL lastRow = FALSE;
        if ([tableView numberOfRowsInSection:[indexPath section]] == 1 ) {
            lastRow = TRUE;
        }       
        // Get the possesion from the row
        Possession *p = [[[myTableSections objectAtIndex:[indexPath section]] objectForKey:@"Possessions"] objectAtIndex:[indexPath row]];

        // delete it from the PossessionStore
        [[[PossessionStore defaultStore] allPossessions] removeObjectIdenticalTo:p];

        // Rebuild myTableViewSections to reflect the data change to the PossessionStore
        [self divideSections];

            // remove the row or section if last row from the table view with an animation
        if (lastRow) {
            // NSLog(@"Should be deleting section");
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES];
        } else {
            // NSLog(@"Should be deleting row");
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
    }
}
相关问题