gpt4 book ai didi

iphone - UITableView insertRowsAtIndexPaths :

转载 作者:行者123 更新时间:2023-11-29 04:49:42 26 4
gpt4 key购买 nike

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

第一次一切正常,然后当我按下后退按钮并选择一个新行或同一行以查看详细信息时,一旦调用 insertRowsAtIndexPaths: ,我会得到以下信息错误:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的行数无效。更新 (2) 后现有节中包含的行数必须为等于更新前该部分中包含的行数 (2),加上或减去从该部分插入或删除的行数(插入 1,删除 0),并加上或减去移入或移出的行数该部分(0 人移入,0 人移出)。'

以下是将 View 插入堆栈的代码:

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 的计数。

关于iphone - UITableView insertRowsAtIndexPaths :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com