简体   繁体   English

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

[英]UITableView adding/deleting sections while not in editing mode?

I have a UITableView and basically I'm making some in app settings, and if the first section's UISegmentedControl is toggled to index 1, then I want to display a new section, but if index 1 was previously set and the user selects index 0 then I need to remove section 2. 我有一个UITableView ,基本上我在应用程序设置中做了一些,如果第一部分的UISegmentedControl切换到索引1,那么我想显示一个新的部分,但如果先前设置了索引1并且用户选择索引0然后我需要删除第2节。

To do this I had this code set to fire on the UISegmentedControl's valueChanged event 为此,我将此代码设置为触发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];
 }

Where the NSMutableArray called sections is the list of all sections. NSMutableArray调用的sections是所有部分的列表。 Each section only has 1 row so no sub arrays are needed. 每个部分只有1行,因此不需要子数组。

However when evaluating the else part I get this error: 但是在评估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).'

I've verified that it had 4 sections prior to the else, it correctly added the two sections to the sections array, I told it the proper indexPaths to the sections added. 我已经验证它在else之前有4个部分,它正确地将两个部分添加到sections数组中,我告诉它添加的部分的正确indexPaths。 Why doesn't this work? 为什么这不起作用?

I tried replacing the [self.tableView insertRows/deleteRows...] line with [self.tableView reloadData]; 我尝试用[self.tableView reloadData];替换[self.tableView insertRows/deleteRows...][self.tableView reloadData]; and then it works fine, but I want to animate adding/deleting those sections. 然后它工作正常,但我想动画添加/删除这些部分。

Update I tried this suggestion and adding works but I'm getting crashing on removing 更新我尝试了这个建议并添加了工作,但我正在崩溃删除

[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];

I get this error. 我收到这个错误。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -
    [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds 
    maximum index value of NSNotFound - 1'

The FT_AND_IN and FRACTION_PRECISION objects are only added/removed from the data store in this code, and they are just const NSString objects. FT_AND_INFRACTION_PRECISION对象只添加/从这个代码数据存储中移除,而他们只是const NSString对象。

It's very hard to read your unformatted code there. 在那里阅读你的无格式代码非常困难。

You want to look at -[UITableView insertSections:withRowAnimation:] and -[UITableView deleteSections:withRowAnimation] , I think. 你想看看-[UITableView insertSections:withRowAnimation:]-[UITableView deleteSections:withRowAnimation] ,我想。

Try: 尝试:

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM