iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – UITableView在不处于编辑模式时添加/删除部分?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITableView,基本上我在应用程序设置中做了一些,如果第一部分的UISegmentedControl切换到索引1,那么我想显示一个新的部分,但如果先前设置了索引1并且用户选择索引0然后我需要删除第2节.

为此,我将此代码设置为触发UISegmentedControl的valueChanged事件

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行,因此不需要子数组.

但是在评估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: inval@R_489_4207@ 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_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];
}

大佬总结

以上是大佬教程为你收集整理的iphone – UITableView在不处于编辑模式时添加/删除部分?全部内容,希望文章能够帮你解决iphone – UITableView在不处于编辑模式时添加/删除部分?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。