0
votes

Initially I need to list 3 Items in tableview.
On Click of any of it it show 3more ITems as sub Item of selected Main Item.

Array I am using is

aryTemp = [[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"YES", @"isExpand", [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil], @"data", nil], nil];
aryTemp1 = [[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"NO", @"isExpand", [NSArray arrayWithObjects:@"Doc", @"XML", @"PDF", nil], @"data", nil], nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        int count = 0;    
        if(!boolIsExpanded)
            count = 1;
        else {
            count = [[[aryTemp objectAtIndex:0] objectForKey:@"data"] count] + [[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count];
        }
        return count;
    }

    -(void)addOrDeleteRows:(NSIndexPath *)indexPath add:(BOOL)aBool
    {
    NSMutableArray *paths = [[NSMutableArray alloc] init];

    if(boolIsExpanded)
    {
        boolIsExpanded=NO;
        for (int i=1; i<=[[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) {
            NSIndexPath *indxPath;
            indxPath = [NSIndexPath indexPathForRow:(indexPath.row)+i inSection:indexPath.section];
            [paths addObject:indxPath];
            NSLog(@"paths : %@",paths);
        }
        intPrevIndex = indexPath.row;
        [tblView beginUpdates];
        [tblView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
        [tblView endUpdates];
    }
    else 
    {
         boolIsExpanded = YES;
        for (int i = 1; i <= [[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) {
            NSIndexPath *indxPath = [NSIndexPath indexPathForRow:(indexPath.row)+i inSection:indexPath.section];
            [paths addObject:indxPath];
        }
        intPrevIndex = indexPath.row;
        [tblView beginUpdates];
        [tblView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
        [tblView endUpdates];
    }    
    }

How exactly this calculation should be done? when using sections.

After inserting rows It calles NumberOfRow and get crash with error Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037 2012-10-03 11:37:43.955 ExpandRowsSample[1657:f803] 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 (6) must be equal to the number of rows contained in that section before the update (1), 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).'*

1

1 Answers

0
votes

During the updates of your table view, you must also modify its data source accordingly, which as I understand from your - tableView:numberOfRowsInSection: is aryTemp.

Therefore, during add, you should have some [aryTemp addObject:...]; calls before the [tblView endUpdates]; line, something like:

for (int i=1; i<=[[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) {
{
    // your current code
    [aryTemp addObject:...];
}

intPrevIndex = indexPath.row;
[tblView beginUpdates];
[tblView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
[tblView endUpdates];

As for deleting rows, the same concept should be applied.