jam*_*one 5 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被叫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 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对象.
在那里阅读未格式化的代码非常困难。
我想你想看看-[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];
}
| 归档时间: | 
 | 
| 查看次数: | 5766 次 | 
| 最近记录: |