0

Hi i have tried to apply multiple arrays in expandable tableview.

but when i run my code it's showing exception.

exception:'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)

how to solve this exception (or) any other example is available please suggest me.

please help me.

My Code:

.m file

#import "ExpandableListviewWithServices.h"

@interface ExpandableListviewWithServices (){

    BackGroundPostServiceClass3 * back;
    NSMutableArray *arrayForBool,*totalListOfArray,*respArray;    
    NSArray *sectionTitleArray;
    UITableView *tableList;

}

@end

@implementation ExpandableListviewWithServices

- (void)viewDidLoad {
    [super viewDidLoad];    

    tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableList.translatesAutoresizingMaskIntoConstraints = NO;
    tableList.dataSource=self;
    tableList.delegate=self;
    tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    tableList.estimatedRowHeight = 44.0;
    tableList.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:tableList];

    NSDictionary * views = NSDictionaryOfVariableBindings(tableList);

    NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableList]-0-|" options:0 metrics:nil views:views];

    NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableList]-0-|"options:0 metrics:nil views:views];

    [self.view addConstraints:horizentalConstraint];
    [self.view addConstraints:verticalConstraint];    

   [self initialization]; 
}

-(void)initialization
{
    arrayForBool=[[NSMutableArray alloc]init];

    sectionTitleArray=[[NSArray alloc]initWithObjects:
                       @"Apple",
                       @"Strawberry",
                       @"Grapes",
                       @"Orange",
                       @"Banana",
                       nil];    

    NSArray *new0=[[NSArray alloc]initWithObjects:@"Apple", @"Strawberry",@"Grapes",@"Orange", nil];

    NSArray *new1=[[NSArray alloc]initWithObjects:@"Apple1", @"Strawberry1",@"Grapes1",@"Orange1"@"Orange1", nil];

    NSArray *new2=[[NSArray alloc]initWithObjects:@"Apple2", @"Strawberry2",@"Grapes2",@"Orange2", nil];

    NSArray *new3=[[NSArray alloc]initWithObjects:@"Apple3", @"Strawberry3",@"Grapes3",@"Orange3",@"Orange3", nil];

    NSArray *new4=[[NSArray alloc]initWithObjects:@"Apple4", @"Strawberry4",@"Grapes4",@"Grapes4", nil];

    totalListOfArray=[[NSMutableArray alloc]init];

    [totalListOfArray addObject:new0];
    [totalListOfArray addObject:new1];
    [totalListOfArray addObject:new2];
    [totalListOfArray addObject:new3];
    [totalListOfArray addObject:new4];

    for (int i=0; i<[sectionTitleArray count]; i++) {

        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if ([[arrayForBool objectAtIndex:section] boolValue]) {        

        return [respArray count];

    }

    else

        return 0;

}

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

    static NSString *CellIdentifier = @"cell2";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    BOOL manyCells  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

    //    /********** If the section supposed to be closed *******************/
    if(!manyCells)
    {
        cell.backgroundColor=[UIColor clearColor];

        cell.textLabel.text=@"";
    }
    /********** If the section supposed to be Opened *******************/
    else
    {
        cell.textLabel.text=[NSString stringWithFormat:@"%@",[respArray objectAtIndex:indexPath.row]];
        cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
        cell.backgroundColor=[UIColor whiteColor];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }

    cell.textLabel.textColor=[UIColor blackColor];

    /********** Add a custom Separator with cell *******************/
    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, tableList.frame.size.width, 1)];
    separatorLineView.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:separatorLineView];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionTitleArray count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    /*************** Close the section, once the data is selected ***********************************/
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

    [tableList reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
        return 40;
    }
    return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80;

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *sectionView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,40)];
    sectionView1.backgroundColor = [UIColor whiteColor];

    return  sectionView1;
}

#pragma mark - Creating View for TableView Section
//# #e0e0eb place this color

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    UIView *headerview = [[UIView alloc] init];
    headerview.backgroundColor = [UIColor lightGrayColor];
    headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80);
    headerview.tag=section;

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, tableList.frame.size.width, 25)];
    label1.text = @"Hi";
    [headerview addSubview:label1];

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerview addGestureRecognizer:headerTapped];

    return headerview;

}

#pragma mark - Table header gesture tapped

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{    


    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

    NSLog(@" index ---->>> %ld",(long)indexPath.section);

    for (int i=0; i<[sectionTitleArray count]; i++){

        if (indexPath.section == i) {

            BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
            for (int i=0; i<[sectionTitleArray count]; i++) {
                if (indexPath.section==i) {
                    [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
                }else{
                    [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:collapsed]];
                }
            }
            [self selectedOption:(int)indexPath.section];

            [tableList reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
}

-(NSMutableArray *)selectedOption:(int)mySelectedItemData{

    NSLog(@"%d",mySelectedItemData);
    NSMutableArray *arrayValue=[[NSMutableArray alloc]init];
    respArray=[[NSMutableArray alloc]init];

    NSLog(@"%@",[totalListOfArray objectAtIndex:mySelectedItemData]);
    NSLog(@"%d",[[totalListOfArray objectAtIndex:mySelectedItemData] count]);

    for (int i=0; i< [[totalListOfArray objectAtIndex:mySelectedItemData]count]; i++) {

        [arrayValue addObject:[[totalListOfArray objectAtIndex:mySelectedItemData] objectAtIndex:i]];
        //[arrayValue addObject:[itemsArray objectAtIndex:mySelectedItemData]];
        NSLog(@"array value is =======> %@",arrayValue);
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }

    respArray=[arrayValue mutableCopy];
    NSLog(@"respArray items are =====> %@",respArray);

    return arrayValue;
}
  • If you're changing the number of rows in a section, you have to add/delete the rows. You can't just call `reloadSections`. – spongessuck Sep 13 '16 at 01:53
  • Hi spongessuck,but i need my sections also reload –  Sep 13 '16 at 02:02
  • The `insertRowsWithIndexPaths` and `deleteRowsWithIndexPaths` methods will update your sections, unless you need to update the section header view, which it doesn't seem like you're doing here. – spongessuck Sep 13 '16 at 02:04
  • Oh- are you getting this error after you open a second section? Try calling `reloadSections` on both the section you're collapsing and the one you're opening. If you need to do it in two steps, you need to put them in between `[tableList beginUpdates]` and `[tableList endUpdates]`. – spongessuck Sep 13 '16 at 02:09
  • how to call reload sections,can you explain me. –  Sep 13 '16 at 02:15
  • You're already doing it: `[tableList reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];` You just need to also call this on the previously open section you're collapsing. – spongessuck Sep 13 '16 at 02:16
  • previously means on view for header in section –  Sep 13 '16 at 02:19
  • i put the reload sections in between begin updates and end updates.still same error showing –  Sep 13 '16 at 02:25
  • Because you're still not doing a `reloadSection` on the section you're collapsing. – spongessuck Sep 13 '16 at 02:30
  • can you show me where it is exactly.please.. –  Sep 13 '16 at 02:35
  • i am doing with reload sections. but same problem repeat –  Sep 13 '16 at 02:40
  • Please clear all unnecessary code from your question, it take long times to read it, your problem is probably you reload before you adding new data to your dataSource, thats why you got that problem, check your dataSource again before reload the section – Tj3n Sep 13 '16 at 03:37

1 Answers1

0

I am placing the below functions with code change which you have done. See, understand and replace the function with your code.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSArray *arrayCount = [totalListOfArray objectAtIndex:section];

    return [arrayCount count];
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{

      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

        NSLog(@" index ---->>> %ld",(long)indexPath.section);

        for (int i=0; i<[sectionTitleArray count]; i++){

            if (indexPath.section == i) {

                BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

                for (int i=0; i<[sectionTitleArray count]; i++) {

                    if (indexPath.section==i) {

                        if (collapsed == NO) {

                            [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:YES]];
                        }
                        else{

                             [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
                        }


                    }else{

                        NSLog(@"%@",arrayForBool);

                        [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
                    }
                }
                [self selectedOption:(int)indexPath.section];
            }
        }
}
-(NSMutableArray *)selectedOption:(int)mySelectedItemData{

    NSLog(@"%d",mySelectedItemData);
    NSMutableArray *arrayValue=[[NSMutableArray alloc]init];
    respArray=[[NSMutableArray alloc]init];

    NSLog(@"%@",[totalListOfArray objectAtIndex:mySelectedItemData]);
    NSLog(@"%lu",(unsigned long)[[totalListOfArray objectAtIndex:mySelectedItemData] count]);

    for (int i=0; i< [[totalListOfArray objectAtIndex:mySelectedItemData]count]; i++) {

        [arrayValue addObject:[[totalListOfArray objectAtIndex:mySelectedItemData] objectAtIndex:i]];
        NSLog(@"array value is =======> %@",arrayValue);
    }

    respArray=[arrayValue mutableCopy];

    [tableList reloadSections:[NSIndexSet indexSetWithIndex:mySelectedItemData] withRowAnimation:UITableViewRowAnimationAutomatic];


    NSLog(@"respArray items are =====> %@",respArray);

    return arrayValue;
}
Jaimish
  • 629
  • 5
  • 15