iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 无效更新:第0节中的无效行数,以NSException类型的未捕获异常终止大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我已经阅读了有关此内容的所有相关帖子,但仍然有错误: ‘Invalid update: invalid number of rows in section 0. the number of rows contained in an exisTing section after the update (5) must be equal to the number of rows contained
我已经阅读了有关此内容所有相关帖子,但仍然有错误

以下是详细信息:

我打开单元格时使用代码打开钢琴表它给了子单元格每行有不同数量的单元格它工作得很好但我需要修改一些东西,当我点击它打开的行然后如果我点击另一行我需要它打开第二个并关闭一个打开的行
这是我的代码
rootView.m

#pragma -mark TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    GCRetractableSectionController* sectionController = [self.retractableControllers objectATindex:section];
    //    NSLog(@"sectionController.numberOfRow %d",sectionController.numberOfRow );
    return sectionController.numberOfRow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath {

    GCRetractableSectionController* sectionController = [self.retractableControllers objectATindex:indexPath.section];
    return [sectionController cellForRow:indexPath.row];
}

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath {
    NSLog(@"index path section %d indexrow %d",indexPath.section,indexPath.row);

    GCRetractableSectionController* sectionController = [self.retractableControllers objectATindex:indexPath.section];


  //  vc.open = !vc.open;


//    return [sectionController didSELEctCellAtRow:indexPath.row];



    if (indexPath.row == 1) {
        [myCustomView setHidden:YES];
           NSLog(@"index path 1111@R_607_8064@1@R_607_8064@111 indexrow %d",indexPath.row);



    }
    else if (indexPath.row == 2) {
    }

    return [sectionController didSELEctCellAtRow:indexPath.row];




}

// GCRetractableSectionController.m

#import "GCRetractableSectionController.h"
#import "RootViewController.h"

@interface GCRetractableSectionController ()

@property (nonatomic,assign) UIViewController *viewController;

- (void) setAccessoryViewOnCell:(UITableViewCell*) cell;

@end

@implementation GCRetractableSectionController

@synthesize uSEOnlyWhiteImages,titleTextColor,titleAlternativeTextColor;
@synthesize viewController;
@synthesize open,rowAnimation;
@synthesize BACkUp;
#pragma mark -
#pragma mark Initialisation

- (id) initWithViewController:(UIViewController*) givenViewController {
    if ((self = [super init])) {
        if (![givenViewController respondsToSELEctor:@SELEctor(tableView)]) {
            //The view controller MUST have a tableView proprety
            [NSException raise:@"Wrong view controller" 
                        format:@"The passed view controller to GCRetractableSectionController must respond to the tableView proprety"];
        }
        self.viewController = givenViewController;
    //  self.open = NO;
        self.uSEOnlyWhiteImages = NO;
        self.rowAnimation = UITableViewRowAnimationTop;

    }
    return self;
}

#pragma mark -
#pragma mark Getters

- (UITableView*) tableView {
    return [self.viewController performSELEctor:@SELEctor(tableView)];
}

- (NSUInteger) numberOfRow {
    NSLog(@"the count %d",self.contentnumberOfRow);

    return (self.open) ? self.contentnumberOfRow + 1 : 1;
}

- (NSUInteger) contentnumberOfRow {
    return 0;
}

- (NSString*) title {
    return NSLocalizedString(@"No title",);
}

- (NSString*) titleContentForRow:(NSUInteger) row {
    return NSLocalizedString(@"No title",);
}

#pragma mark -
#pragma mark Cells

- (UITableViewCell *) cellForRow:(NSUInteger)row {
    UITableViewCell* cell = nil;

    if (row == 0) cell = [self titleCell];
    else cell = [self contentCellForRow:row - 1];

    return cell;
}

- (UITableViewCell *) titleCell {
    NSString* titleCellIdentifier = [NSStringFromClass([self class]) StringByAppendingString:@"title"];

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier@R_447_6964@CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reusEIDentifier@R_447_6964@CellIdentifier];
    }

    cell.textLabel.text = self.title;
    if (self.contentnumberOfRow != 0) {
        cell.detailTextLabel.text = [NSString StringWithFormat:NSLocalizedString(@"%i items",),self.contentnumberOfRow];
        cell.SELEctionStyle = UITableViewCellSELEctionStyleBlue;
        [self setAccessoryViewOnCell:cell];
    }
    else {
        cell.detailTextLabel.text = NSLocalizedString(@"No item",);
        cell.SELEctionStyle = UITableViewCellSELEctionStyleNone;
        cell.accessoryView = nil;
        cell.textLabel.textColor = [UIColor blackColor];
    }

    return cell;
}

- (UITableViewCell *) contentCellForRow:(NSUInteger)row {
    NSString* contentCellIdentifier = [NSStringFromClass([self class]) StringByAppendingString:@"content"];

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:contentCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reusEIDentifier:contentCellIdentifier] ;
        cell.accessoryType = UITableViewCellAccessoryDisclosureInDicator;
    }

    cell.textLabel.text = [self titleContentForRow:row];

    return cell;
}

- (void) setAccessoryViewOnCell:(UITableViewCell*) cell {
    NSString* path = nil;
    if (self.open) {
        path = @"UpAccessory";
        if (self.titleAlternativeTextColor == nil) cell.textLabel.textColor =  [UIColor colorWithRed:0.191 green:0.264 blue:0.446 alpha:1.000];
        else cell.textLabel.textColor = self.titleAlternativeTextColor;
    }   
    else {
        path = @"DownAccessory";
        cell.textLabel.textColor = (self.titleTextColor == nil ? [UIColor blackColor] : self.titleTextColor);
    }

    UIImage* accessoryImage = [UIImage imagenamed:path];
    UIImage* whiteAccessoryImage = [UIImage imagenamed:[[path StringBydeletingPathExtension] StringByAppendingString:@"White"]];

    UIImageView* imageView;
    if (cell.accessoryView != nil) {
        imageView = (UIImageView*) cell.accessoryView;
        imageView.image = (self.uSEOnlyWhiteImages ? whiteAccessoryImage : accessoryImagE);
        imageView.highlightedImage = whiteAccessoryImage;
    }
    else {
        imageView = [[UIImageView alloc] initWithImage:(self.uSEOnlyWhiteImages ? whiteAccessoryImage : accessoryImagE)];
        imageView.highlightedImage = whiteAccessoryImage;
        cell.accessoryView = imageView;
    }
}

#pragma mark -
#pragma mark SELEct Cell

- (void) didSELEctCellAtRow:(NSUInteger)row {
    if (row == 0) {
           [self didSELEcttitleCell];
    }
    else [self didSELEctContentCellAtRow:row - 1];
}

- (void) didSELEcttitleCell {


    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *myDecodedObject  = [userDefaults objectForKey:@"tableViewDataPrevIoUs"];
    NSMutableArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
    NSLog(@"the BACk Up Count %d",[decodedArray count]);
    if ([decodedArray count]!=0){

        NSLog(@"BACk Up Array ");
        NSLog(@"am here herer hererer hererer %@ ",decodedArray);



        // it quit here
       [self.tableView deleteRowsATindexPaths:decodedArray withRowAnimation:self.rowAnimation];
        //


        [self.tableView reloadData];

        [self.tableView endupdates];

//        NSString *appDomain = [[NSBundle mainBundle] bundlEIDentifier];
//        [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
        NSLog(@"am here herer hererer hererer");

    }

    NSLog(@"didSELEctedtitleCell");
    self.open = !self.open;
    NSLog(@"1");
    if (self.contentnumberOfRow != 0) [self setAccessoryViewOnCell:[self.tableView cellForRowATindexPath:[self.tableView indexPathForSELEctedRow]]];

    NSIndexPath* indexPath = [self.tableView indexPathForSELEctedRow];

    NSUInteger section = indexPath.section;
    NSUInteger contentCount = self.contentnumberOfRow;

    [self.tableView beginupdates];

    NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
    for (NSUInteger i = 1; i < contentCount + 1; i++) {
        NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
        [rowToInsert addObject:indexPathToInsert];
    }

    if (self.open) {


        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
         NSData *myDecodedObject  = [userDefaults objectForKey:@"tableViewDataPrevIoUs"];
        NSMutableArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];

        NSLog(@"the BACk Up Count %d",[decodedArray count]);

        if ([decodedArray count]!=0){

            NSLog(@"BACk Up Array ");
         [self.tableView deleteRowsATindexPaths:decodedArray withRowAnimation:self.rowAnimation];


             //[self.tableView endupdates];

            NSString *appDomain = [[NSBundle mainBundle] bundlEIDentifier];
            [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];


        }
             [self.tableView insertRowsATindexPaths:rowToInsert withRowAnimation:self.rowAnimation];

        [self.tableView endupdates];



         BACkUp = [[NSMutableArray alloc]init];
        [BACkUp addObjectsFromArray:rowToInsert];
         NSLog(@"the BACk Up Count %@",BACkUp );
        NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:BACkUp];

        [userDefaults setObject:myEncodedObject forKey:@"tableViewDataPrevIoUs"];
        ;
        [userDefaults synchronize];




    }
    else {
         [self.tableView deleteRowsATindexPaths:rowToInsert withRowAnimation:self.rowAnimation];
        [self.tableView endupdates];
}


    [self.tableView endupdates];

    if (self.open) [self.tableView scrollToNearestSELEctedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
    [self.tableView deSELEctRowATindexPath:indexPath animated:YES];
}

- (void) didSELEctContentCellAtRow:(NSUInteger)row {

}

@end

输出

2014-10-09 13:10:13.747 Brnlysta2[10199:90b] the BACk Up Count (
        "<NSIndexPath: 0x8e8fb80> {length = 2,path = 0 - 1}","<NSIndexPath: 0x8e87900> {length = 2,path = 0 - 2}","<NSIndexPath: 0x8e871c0> {length = 2,path = 0 - 3}","<NSIndexPath: 0x8e908e0> {length = 2,path = 0 - 4}"
    )
    2014-10-09 13:10:14.717 Brnlysta2[10199:90b] index path section 1 indexrow 0
    2014-10-09 13:10:14.718 Brnlysta2[10199:90b] the BACk Up Count 4
    2014-10-09 13:10:14.718 Brnlysta2[10199:90b] BACk Up Array 
    2014-10-09 13:10:14.719 Brnlysta2[10199:90b] am here herer hererer hererer (
        "<NSIndexPath: 0x8b3bb60> {length = 2,"<NSIndexPath: 0x8b8e5c0> {length = 2,"<NSIndexPath: 0x8b75f60> {length = 2,"<NSIndexPath: 0x8b7b960> {length = 2,path = 0 - 4}"
    ) 
    2014-10-09 13:10:14.719 Brnlysta2[10199:90b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:],/sourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368
    2014-10-09 13:10:14.722 Brnlysta2[10199:90b] *** TerminaTing app due to uncaught exception 'NSInternalInconsistencyException',reason: 'Invalid update: invalid number of rows in section 0.  the number of rows contained in an exisTing section after the update (5) must be equal to the number of rows contained in that section before the update (5),plus or minus the number of rows inserted or deleted from that section (0 inserted,4 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in,0 moved out).'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x017fe1e4 __exceptionPreprocess + 180
        1   libobjc.A.dylib                     0x0157d8e5 objc_exception_throw + 44
        2   CoreFoundation                      0x017fe048 +[NSException raise:format:arguments:] + 136
        3   Foundation                          0x0115d4de -[NSAssertionHandler handleFailureInMethod:object:file:linenumber:description:] + 116
        4   UIKit                               0x00315f63 -[UITableView _endCellAnimationsWithContext:] + 13402
        5   UIKit                               0x00326590 -[UITableView _updateRowsATindexPaths:updateAction:withRowAnimation:] + 337
        6   UIKit                               0x0032660b -[UITableView deleteRowsATindexPaths:withRowAnimation:] + 55
        7   Brnlysta2                           0x0000368e -[GCRetractableSectionController didSELEcttitleCell] + 382
        8   Brnlysta2                           0x000034d7 -[GCRetractableSectionController didSELEctCellAtRow:] + 71
        9   Brnlysta2                           0x00008f13 -[RootViewController tableView:didSELEctRowATindexPath:] + 531
        10  UIKit                               0x003279a1 -[UITableView _SELEctRowATindexPath:animated:scrollPosition:notifyDelegate:] + 1513
        11  UIKit                               0x00327b14 -[UITableView _userSELEctRowAtPendingSELEctionIndexPath:] + 279
        12  UIKit                               0x0032c10e __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
        13  UIKit                               0x0025b0aa ___afterCACommitHandler_block_invoke + 15
        14  UIKit                               0x0025b055 _applyBlockToCFArrayCopiedToStack + 403
        15  UIKit                               0x0025ae76 _afterCACommitHandler + 532
        16  CoreFoundation                      0x017c636e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
        17  CoreFoundation                      0x017c62bf __CFRunLoopDoObservers + 399
        18  CoreFoundation                      0x017a4254 __CFRunLoopRun + 1076
        19  CoreFoundation                      0x017a39d3 CFRunLoopRunSpecific + 467
        20  CoreFoundation                      0x017a37eb CFRunLoopRunInMode + 123
        21  Graphicsservices                    0x037a25ee GSEventRunModal + 192
        22  Graphicsservices                    0x037a242b GSEventRun + 104
        23  UIKit                               0x0023df9b UIApplicationMain + 1225
        24  Brnlysta2                           0x0000669d main + 141
        25  libdyld.dylib                       0x01e43725 start + 0
    )
    libc++abi.dylib: terminaTing with uncaught exception of type NSException

解决方法

当您执行编辑/更新UITableView的操作时,如果数据完整性仍然存在,它会检查一些datasource方法.你有问题.在您的numberOfSectionsInTableView或tableView:numberOfRowsInSection:方法中,存在误算.在调用deleteRowsATindexPaths之前,还需要更新用于计算节/行计数的必要属性.

便说一句,在deleteRowsATindexPaths之后使用reloadData没有任何意义.它可能会干扰您的删除动画.

大佬总结

以上是大佬教程为你收集整理的ios – 无效更新:第0节中的无效行数,以NSException类型的未捕获异常终止全部内容,希望文章能够帮你解决ios – 无效更新:第0节中的无效行数,以NSException类型的未捕获异常终止所遇到的程序开发问题。

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

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