代码之家  ›  专栏  ›  技术社区  ›  jamone

UITableView是否在不处于编辑模式时添加/删除节?

  •  5
  • jamone  · 技术社区  · 16 年前

    我有一个 UITableView 基本上我在做一些应用内设置,如果第一部分 UISegmentedControl

    为了做到这一点,我把这个密码放在了火上 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 打电话 sections 是所有节的列表。每个部分只有一行,因此不需要子数组。

    但是,在评估else部分时,我得到以下错误:

    *** 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).'
    

    部分 数组,我告诉它添加的节的正确索引。为什么这样不行?

    我试着换新的 [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 const NSString 物体。

    1 回复  |  直到 16 年前
        1
  •  4
  •   Seamus Campbell    16 年前

    在那里很难读懂你的未格式化代码。

    -[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];
    }
    
    推荐文章