iOS,试图让insertRowsAtIndexPaths工作

时间:2014-09-22 12:57:30

标签: ios objective-c

我有一个UITableView,当数据从PHP调用返回时最初加载10条记录,当滚动条到达最后一项时,我进行PHP调用以获取下一条记录并在单元格中显示此数据。我正在尝试使用insertRowsAtIndexPaths但是当我点击numberOfRowsInSection应用程序崩溃时我一直收到此错误:

  uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (1) 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 (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

CODE

 -(void)renderScrollThreadInfo:(NSDictionary*)dic{

                NSDictionary *thread = [dic objectForKey:@"thread"];
                countNewThreadsBottom = 0;
                if((NSNull*)thread != [NSNull null]){

                int t_ID;
                    int t_U_ID;
                    int t_C_ID;
                    NSString *t_Name;
                    NSString *t_Description;
                    NSDate *t_Created;
                    int t_Flagged;
                    int t_Rated;
                    NSString *firstName;
                    NSString *lastName;
                    NSString *categoryName;

                NSMutableArray *tmpArray = [[NSMutableArray alloc] init];

                    for(NSDictionary *dict in thread)
                    {
                    if((NSNull *)[dict objectForKey:@"T_ID"] != [NSNull null]){
                    t_ID = [[dict objectForKey:@"T_ID"] intValue];
                    }
                    if((NSNull *)[dict objectForKey:@"T_U_ID"] != [NSNull null]){
                    t_U_ID = [[dict objectForKey:@"T_U_ID"] intValue];
                    }
                    if((NSNull *)[dict objectForKey:@"T_C_ID"] != [NSNull null]){
                    t_C_ID = [[dict objectForKey:@"T_C_ID"] intValue];
                    }
                    if((NSNull *)[dict objectForKey:@"T_Name"] != [NSNull null]){
                    t_Name = [dict objectForKey:@"T_Name"];
                    }
                    if((NSNull *)[dict objectForKey:@"T_Description"] != [NSNull null]){
                    t_Description = [dict objectForKey:@"T_Description"];
                    }
                    if((NSNull *)[dict objectForKey:@"T_Created"] != [NSNull null]){
                    NSString *timestampString = [dict objectForKey:@"T_Created"];
                    double timestampDate = [timestampString doubleValue];
                    t_Created = [NSDate dateWithTimeIntervalSince1970:timestampDate];
                    }
                    if((NSNull *)[dict objectForKey:@"T_Flagged"] != [NSNull null]){
                    t_Flagged = [[dict objectForKey:@"T_Flagged"] intValue];
                    }
                    if((NSNull *)[dict objectForKey:@"T_Rated"] != [NSNull null]){
                    t_Rated = [[dict objectForKey:@"T_Rated"] intValue];
                    }
                    if((NSNull *)[dict objectForKey:@"U_FirstName"] != [NSNull null]){
                    firstName = [dict objectForKey:@"U_FirstName"];
                    }
                    if((NSNull *)[dict objectForKey:@"U_LastName"] != [NSNull null]){
                    lastName = [dict objectForKey:@"U_LastName"];
                    }
                    if((NSNull *)[dict objectForKey:@"C_Name"] != [NSNull null]){
                    categoryName = [dict objectForKey:@"C_Name"];
                    }

                    ThreadInfo *threadObj = [ThreadInfo new];
                    threadObj.iD = t_ID;
                    threadObj.userId  = t_U_ID;
                    threadObj.catId = t_C_ID;
                    threadObj.name = t_Name;
                    threadObj.description = t_Description;
                    threadObj.timeStampCreated = t_Created;
                    threadObj.flagged = t_Flagged;
                    threadObj.rated = t_Rated;
                    threadObj.firstName = firstName;
                    threadObj.lastName = lastName;
                    threadObj.category = categoryName;

                    c = true;
                    [tmpArray addObject:[NSIndexPath indexPathForRow:countNewThreadsBottom inSection:1]];
                    countNewThreadsBottom += 1;

                    [tableViewThreads beginUpdates];
                    [tableViewThreads insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
                    [tableViewThreads endUpdates];

                    [threadsArray addObject:threadObj];
                }


                //[tableViewThreads reloadData];
                }

            }

            - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
            {
                return 2;
            }

            -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
                if(section == 0){
                if(tableViewThreads == tableView){

                    NSLog(@"xfhdh %d", [threadsArray count]);
                    return 10;

                }

                }else{
                if(tableViewThreads == tableView){

                    return 1;

                }

                }

                if(tableViewPosts == tableView){
                return [postsArray count];
                }
                return 0;
            }

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


                if(tableViewThreads == tableView){
                NSString *cellIdentifier = @"cell";
                ThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

                int sec = indexPath.section;

                ThreadInfo *threadInfo;

                if(indexPath.section == 1){
                    threadInfo = (ThreadInfo*)[self.threadsArray objectAtIndex:[threadsArray count] - 1];

                    if (cell == nil)
                    {
                    cell = [[ThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

                    [cell setupView:threadInfo];

                    }



                    cell.labelFirstName.text = [NSString stringWithFormat:@"%@ %@", threadInfo.firstName,threadInfo.lastName];

                    cell.labelTimestamp.text = [NSDateFormatter localizedStringFromDate:threadInfo.timeStampCreated dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
                    cell.labelTimestamp.text = [cell.labelTimestamp.text stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                    cell.labelTimestamp.text = [cell.labelTimestamp.text stringByReplacingOccurrencesOfString:@"PM" withString:@""];

                    cell.labelThreadName.text = threadInfo.name;
                }else{
                    threadInfo = (ThreadInfo*)[self.threadsArray objectAtIndex:indexPath.row];

                    if (cell == nil)
                    {
                    cell = [[ThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

                    [cell setupView:threadInfo];

                    }



                    cell.labelFirstName.text = [NSString stringWithFormat:@"%@ %@", threadInfo.firstName,threadInfo.lastName];

                    cell.labelTimestamp.text = [NSDateFormatter localizedStringFromDate:threadInfo.timeStampCreated dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
                    cell.labelTimestamp.text = [cell.labelTimestamp.text stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                    cell.labelTimestamp.text = [cell.labelTimestamp.text stringByReplacingOccurrencesOfString:@"PM" withString:@""];

                    cell.labelThreadName.text = threadInfo.name;


                }

                if(sec == 1){
                    int r = indexPath.row;
                    int g = 0;
                }


                //cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;

                }
                if(tableViewPosts == tableView){
                NSString *cellIdentifier = @"cell2";
                PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

                PostInfo *postInfo = (PostInfo*)[self.postsArray objectAtIndex:indexPath.row];
                if (cell == nil)
                {
                    cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

                    [cell setupView:postInfo];
                }



                cell.labelUserName.text = [NSString stringWithFormat:@"%@ %@ posted...", postInfo.firstName,postInfo.lastName];
                cell.labelCreated.text = [NSDateFormatter localizedStringFromDate:postInfo.timeStampCreated dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
                cell.labelCreated.text = [cell.labelCreated.text stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                cell.labelCreated.text = [cell.labelCreated.text stringByReplacingOccurrencesOfString:@"PM" withString:@""];
                cell.labelMessage.text = postInfo.message;
                return cell;
                //[cell.contentView addSubview:[self setupThreadItem:threadInfo]];
                }

                return nil;
            }

            - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
                NSString *cellIdentifier = @"cell2";
                if(tableViewThreads == tableView){

                return 122;

                }
                if(tableViewPosts == tableView){
                PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                CGFloat h;
                PostInfo *postInfo = (PostInfo*)[self.postsArray objectAtIndex:indexPath.row];
                if (cell == nil){

                    cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
                    [cell setupView:postInfo];
                }
                cell.labelUserName.text = [NSString stringWithFormat:@"%@ %@ posted...", postInfo.firstName,postInfo.lastName];
                cell.labelCreated.text = [NSDateFormatter localizedStringFromDate:postInfo.timeStampCreated dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
                cell.labelCreated.text = [cell.labelCreated.text stringByReplacingOccurrencesOfString:@"AM" withString:@""];
                cell.labelCreated.text = [cell.labelCreated.text stringByReplacingOccurrencesOfString:@"PM" withString:@""];
                cell.labelMessage.text = postInfo.message;

                [cell setNeedsLayout];
                [cell layoutIfNeeded];

                h = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;

                return h;

                }
                return 0;
            }


            -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
                return 60;
            }


            - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
            {
                if(tableViewThreads == tableView){
                //NSLog(@"%d %d",[indexPath row], [threadsArray count] - 1);
                if ([indexPath row] == [threadsArray count] - 2) {

                    ThreadInfo *threadInfo = (ThreadInfo*)[threadsArray objectAtIndex:[threadsArray count] - 1];
                    int tid = threadInfo.iD;


                    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

                    // 3) Load picker in background
                    dispatch_async(concurrentQueue, ^{


                    NSString *searchItem = textFieldSearchThreads.text;
                    NSString *myRequestString = [NSString stringWithFormat:@"Category=%d&TID=%d&SearchItem=%@",rowCategory, tid, searchItem];
                    NSString *response = [self setupPhpCall:myRequestString :@"getThread.php"];


                    dispatch_async(dispatch_get_main_queue(), ^{

                        if(response.length > 0){
                        [self renderScrollThreadInfo:[response JSONValue]];
                        }

                    });

                    });
                }
                }

                if(tableViewPosts == tableView){
                //NSLog(@"%d %d",[indexPath row], [threadsArray count] - 1);
                if ([indexPath row] == [postsArray count] - 1) {

                    PostInfo *postInfo = (PostInfo*)[postsArray objectAtIndex:[postsArray count] - 1];
                    int pid = postInfo.iD;


                    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

                    // 3) Load picker in background
                    dispatch_async(concurrentQueue, ^{


                    NSString *myRequestString = [NSString stringWithFormat:@"TID=%d&PID=%d", chatThreadId, pid];
                    NSString *response = [self setupPhpCall:myRequestString :@"getStandalonePost.php"];


                    dispatch_async(dispatch_get_main_queue(), ^{

                        if(response.length > 0){
                        //[tableViewPosts scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
                        [self renderScrollPostInfo:[response JSONValue]];
                        }

                    });

                    });
                }
                }
            }

1 个答案:

答案 0 :(得分:0)

我认为你在-numberOfRowsInSection函数中返回的行数不正确。根据条件检查是否返回行数,包括插入的行。