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

Posted

技术标签:

【中文标题】UITableView 在不处于编辑模式时添加/删除部分?【英文标题】:UITableView adding/deleting sections while not in editing mode? 【发布时间】:2010-08-16 23:36:21 【问题描述】:

我有一个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 称为sections 是所有部分的列表。每个部分只有 1 行,因此不需要子数组。

但是,在评估 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).'

我已经验证它在 else 之前有 4 个部分,它正确地将这两个部分添加到 sections 数组中,我告诉它添加的部分的正确 indexPaths。为什么这不起作用?

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

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

[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_INFRACTION_PRECISION 对象仅在此代码中从数据存储中添加/删除,它们只是 const NSString 对象。

【问题讨论】:

看到分数高的人很难过并不会格式化代码 它应该可以正常工作。我看不出有任何问题。你能仔细检查 indexSections 数组吗? 【参考方案1】:

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

你想看看-[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];

【讨论】:

我仍然不确定为什么 myne 的编辑版本不起作用,但您的版本起作用。你的唯一问题是[self.tableView addSections...] 行应该是insertSections。谢谢!

以上是关于UITableView 在不处于编辑模式时添加/删除部分?的主要内容,如果未能解决你的问题,请参考以下文章

编辑模式下的 UITableView - 按 Delete 会使我的应用程序崩溃

UITableView 处于编辑模式时选择行

UITableView 处于编辑模式时如何隐藏栏按钮项? (迅速)

如何在编辑模式下替换 Grouped UITableView 的加号/减号按钮?

UITableView 在进入编辑模式时取消选择单元格

已处于编辑模式时滑动删除