繁体   English   中英

UITableView insertRowsAtIndexPaths:

[英]UITableView insertRowsAtIndexPaths:

我有一个导航控制器,当我按一行时它包含一个uitableview,它会在堆栈上弹出一个新的视图控制器,该控制器用于在详细信息视图中显示详细信息,它向服务器发出请求以获取一些响应信息,然后返回的信息我使用insertRowsAtIndexPaths:来显示从服务器返回的信息。

第一次一切都正常,然后当我insertRowsAtIndexPaths:时,当我按返回按钮并选择新行或同一行以查看详细信息时,出现以下错误:

*** Terminating app due to 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 (2) must be equal to the number of rows contained in that section before the update (2), 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).'

这是用于将视图推入堆栈的代码:

VideoDetailViewController_iPhone *nextView = [[VideoDetailViewController_iPhone alloc] initWithNibName:@"VideoDetailViewController_iPhone" bundle:nil withVideo:rowData];
    nextView.navController = navController;
[navController pushViewController:nextView animated:YES];
[nextView release];

这是从服务器返回信息后执行的代码

    - (void)fetchVideoDetail:(NSNotification *)notification {
        hasLoadedResponses = YES;

        NSArray *obj = (NSArray *)[notification object];
        responses = [[obj valueForKey:@"responses"] mutableCopy];
        //NSLog(@"RESPONSES: %@", responses);
        if ([responses count] == 0) {
            [tblView reloadData];
            return;
        }

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

        int i = 0;
        for (NSArray *x in responses) {
            if (i > 0) {
                //The reason for skipping the first one is because we will change that row once the table refreshes we just need to insert any rows after the first one.
                [indexes addObject:[NSIndexPath indexPathForRow:i inSection:1]];
            }
            i++;
        }
        //NSLog(@"indexCount: %i", [indexes count]);
        [tblView beginUpdates];
        [tblView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationBottom];
        [tblView endUpdates];
        //[tblView reloadData];
    }

这是tableView方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    } else {
        if ([responses count] == 0) {
            NSLog(@"numberofrowsinsection: 1");
            return 1;
        } else {
            NSLog(@"numberofrowsinsection: %i", [responses count]);
            return [responses count];
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoCell *cell = (VideoCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
    if (cell == nil) {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    if (indexPath.section == 0) {
            cell.lblTitle.text = [data title];
            cell.lblDescription.text = [data videoDescription];
    } else {
        if ([responses count] == 0) {
            if (!hasLoadedResponses) {
                cell.lblTitle.text = @"";
                cell.lblDescription.text = @"";
            } else {
                //Responses have been loaded
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                cell.lblTitle.text = @"No responses to this video";
                cell.lblDescription.text = @"Be the first to respond by selecting the \"Set as Destination\" button above";
            }
        } else {
            //Display the response information
            cell.lblTitle.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"title"];
            cell.lblDescription.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"description"];
        }
    }

    return cell;
}

您的数据源和行数不同步。 插入行时,必须同时增加该部分中的行数。 在这种情况下,您将必须增加在numberOfRowsInSection方法中使用的数组responses的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM