应用程序在UITableView更新时崩溃

时间:2012-11-06 12:00:22

标签: iphone uitableview

我在parsingDidEnd解析上实现tableupdate。当用户到达表的末尾并滚动时......我调用解析并在解析完成后... UITableView更新...  它工作得很好,但是当我连续滚动TableView 4-5次时,它崩溃的错误如下:

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).

按如下方式查找我的更新代码:

    #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];
        }

    }}

1 个答案:

答案 0 :(得分:1)

numberofRowsinSection肯定会返回错误的值,如错误说明中所示:

  

第0部分中的行数无效。包含的行数   在现有部分更新后(50)必须等于   该部分中包含的行数在更新前的(40),   加上或减去从该部分插入或删除的行数   ( 20插入,0删除)。

40 + 20!= 50。