abhiTouchmagic
abhiTouchmagic

Reputation: 323

App crashes on UITableView update

I am implementing the tableupdate on parsingDidEnd parsing. When user reach end of the table and scroll... I call the parsing and when parsing is done... UITableView is updated... It works perfectly but when i scroll the TableView continuously for 4-5 time it crashes with error as follows:

2012-11-06 17:19:14.810 MightyGoodDeals[1345:707] *** 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 (50) must be equal to the number of rows contained in that section before the update (40), plus or minus the number of rows inserted or deleted from that section (20 inserted, 0 deleted).

Find my code for update as follows:

    #pragma mark <myLIBXMLParser> Implementation- (void)parserDidEndParsingData:(myLIBXMLParser *)parser {
    //  self.title = [NSString stringWithFormat:@"%i Patients Records", [objects count]];

    //self.navigationItem.rightBarButtonItem.enabled = YES;
    self.parser = nil;
    NSLog(@"%@",[objects description]);
    if ([objects count]==0 ||[[[objects objectAtIndex:0]valueForKey:@"NoData"]isEqualToString:@"0"]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"MightyGoodDeals" message:@"There are no deals to display." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [self removeLoadingView];
    }
    else {
        NSLog(@"in else");
        [self removeLoadingView];

        NSMutableArray *indexArray=[[NSMutableArray alloc]init];
        NSLog(@"%d",[objects count]);
        NSLog(@"intSkip: %d",intSkip);


        for (int i=intSkip; i<[objects count]; i++) 
        {
             NSLog(@"in for loop");
            NSLog(@"%d",i);
            NSIndexPath *path=[NSIndexPath indexPathForRow:i inSection:0];
            [indexArray addObject:path];

        }
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];



        self.tableView.tableFooterView = nil;
        [self.tableView setSeparatorColor:[UIColor blackColor]];


        //[self.tableView reloadData];
        int value=[[[objects objectAtIndex:[objects count]-1]valueForKey:@"Skip"]intValue];
        float remainder = fmod([objects count],value); 
        int endValue=(int)remainder;

        if (endValue==0) 
        {
            syncParse=TRUE;
        }
        else
        {   
            syncParse=FALSE;
        }
    }}
    - (void)parser:(myLIBXMLParser *)parser didParseObjects:(NSArray *)parsedObjects{       [objects addObjectsFromArray:parsedObjects];    }



    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
{   
    CGFloat height = scrollView.frame.size.height;

    CGFloat contentYoffset = scrollView.contentOffset.y;

    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;

    if(distanceFromBottom < height) 
    {
        NSLog(@"end of the table");

        if (syncParse) 
        {
            int value=[[[objects objectAtIndex:[objects count]-1]valueForKey:@"Skip"]intValue];
            intSkip=value;

            [NSThread detachNewThreadSelector:@selector(StartParsing) toTarget:self withObject:nil];

            UIView *loadingViewShow=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

            UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
            [spinner startAnimating];
            spinner.frame = CGRectMake(115, 15, 20,20);
            [loadingViewShow addSubview:spinner];

            UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(140, 0, 160, 44)];
            [lbl setText:@"Loading....."];
            [lbl setBackgroundColor:[UIColor clearColor]];
            [lbl setTextColor:[UIColor grayColor]];
            [loadingViewShow addSubview:lbl];
            self.tableView.tableFooterView = loadingViewShow;

            //[self StartParsing];
        }

    }}

Upvotes: 1

Views: 234

Answers (1)

A-Live
A-Live

Reputation: 8944

numberofRowsinSection certainly returns incorrect value as you can see in the error description:

invalid number of rows in section 0. The number of rows contained in an existing section after the update (50) must be equal to the number of rows contained in that section before the update (40), plus or minus the number of rows inserted or deleted from that section (20 inserted, 0 deleted).

40+20 != 50.

Upvotes: 1

Related Questions