UITableView在不处于编辑模式时添加/删除部分?

时间:2010-08-16 23:36:22

标签: iphone uitableview ipad ios

我有一个UITableView基本上我在应用设置中做了一些,如果第一部分的UISegmentedControl切换到索引1,那么我想显示一个新的部分,但是如果索引先前已设置1并且用户选择索引0然后我需要删除第2部分。

为此,我将此代码设置为UISegmentedControl's valueChanged event

 if (segmentControl.selectedSegmentIndex == 0)
 {
     self.settings.useMetric = YES;
     if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) {

         NSArray *indexSections = [NSArray arrayWithObjects:
             [NSIndexPath indexPathForRow:0 inSection:
                 [sections indexOfObject:FT_AND_IN]], 
             [NSIndexPath indexPathForRow:0 inSection:
                 [sections indexOfObject:FRACTION_PRECISION]], nil];
         [sections removeObject:FT_AND_IN];
         [sections removeObject:FRACTION_PRECISION];
         [self.tableView deleteRowsAtIndexPaths:indexSections
             withRowAnimation:UITableViewRowAnimationRight];
     }
 }
 else {
     self.settings.useMetric = NO;
     [sections insertObject:FT_AND_IN atIndex:1];
     [sections insertObject:FRACTION_PRECISION atIndex:2];
     NSArray *indexSections = [NSArray arrayWithObjects:
         [NSIndexPath indexPathForRow:0 inSection:
             [sections indexOfObject:FT_AND_IN]], 
         [NSIndexPath indexPathForRow:0 inSection:
             [sections indexOfObject:FRACTION_PRECISION]], nil];
     [self.tableView insertRowsAtIndexPaths:indexSections 
         withRowAnimation:UITableViewRowAnimationRight];
 }

名为NSMutableArray的{​​{1}}是所有部分的列表。每个部分只有1行,因此不需要子数组。

然而,在评估else部分时,我收到此错误:

sections

我已经验证它在else之前有4个部分,它正确地将这两个部分添加到*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:904 *** 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 (6) must be equal to the number of sections contained in the table view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 数组中,我告诉它添加的部分的正确indexPaths。为什么这不起作用?

我尝试用sections替换[self.tableView insertRows/deleteRows...]行,然后它运行正常,但我想动画添加/删除这些部分。

更新 我尝试了这个建议并添加了作品,但我正在崩溃中删除

[self.tableView reloadData];

我收到此错误。

[self.tableView beginUpdates];
if (segmentControl.selectedSegmentIndex == 0)
{
        self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && 
            [sections containsObject:FRACTION_PRECISION])
        {

        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:
                [sections indexOfObject:FT_AND_IN]] 
                withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:
                [sections indexOfObject:FRACTION_PRECISION]] 
                withRowAnimation:UITableViewRowAnimationRight];
    }
}
else 
    {
        self.settings.useMetric = NO;
    [sections insertObject:FT_AND_IN atIndex:1];
        [sections insertObject:FRACTION_PRECISION atIndex:2];
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
    NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:2];
    [self.tableView insertSections:indexSet 
            withRowAnimation:UITableViewRowAnimationRight];
    [self.tableView insertSections:indexSet2 
            withRowAnimation:UITableViewRowAnimationRight];
}
[self.tableView endUpdates];

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds maximum index value of NSNotFound - 1' FT_AND_IN个对象仅在此代码中的数据存储中添加/删除,而且只是FRACTION_PRECISION个对象。

1 个答案:

答案 0 :(得分:4)

在那里阅读未格式化的代码非常困难。

我想看看-[UITableView insertSections:withRowAnimation:]-[UITableView deleteSections:withRowAnimation]

尝试:

if (segmentControl.selectedSegmentIndex == 0)
{
    self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) {

        NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
        [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

        [sections removeObject:FT_AND_IN];
        [sections removeObject:FRACTION_PRECISION];

        [self.tableView deleteSections:indexSections
             withRowAnimation:UITableViewRowAnimationRight];
     }
 }
 else {
     self.settings.useMetric = NO;
     [sections insertObject:FT_AND_IN atIndex:1];
     [sections insertObject:FRACTION_PRECISION atIndex:2];

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

     [self.tableView insertSections:indexSections
          withRowAnimation:UITableViewRowAnimationRight];
}