4

我在向 UITableView 添加新行时遇到问题。我在stackoverflow上阅读了类似的问题,用谷歌搜索了它,......并没有帮助我。

我有空的 NSMutableArray *dataForList 和 UITableView 的数据。单击屏幕后,我想添加新行。此错误显示:

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”

代码:

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is zero now
                              inSection:0]];

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

[self.unsignedTableView beginUpdates];
[self.unsignedTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.unsignedTableView endUpdates]; // on this line error ocours

我错过了什么?

所有 UITableView 方法

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {}

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

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return [self.mainController.dataForList count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
return 38.0f;
}

- (UITableViewCell *) tableView: (UITableView *) tableView
      cellForRowAtIndexPath: (NSIndexPath *) indexPath {
// call render method in view, it works fain
return [self.signUpToTestView renderUnsignedCell:tableView cellForRowAtIndexPath:indexPath];
}
4

10 回答 10

7

确保您创建的表视图具有委托和数据源集。

例如:

 [self.unsignedTableView setDelegate:self];
 [self.unsignedTableView setDatasource:self];

我认为这次崩溃的原因是数据源和委托方法没有被调用

于 2012-07-24T05:39:40.450 回答
3

insertRowsAtIndexPaths确保添加行时,您的UITableViewDataSource方法还会返回添加的单元格的新数量和新对象。

检查tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:

否则你会得到一个NSInternalInconsistencyException例外。

如果您尝试使用 insertRowsAtIndexPaths 添加所有单元格,请停止您的操作并阅读内容。

于 2012-04-04T10:23:50.963 回答
3

您是否在单击后根据您的数组更改numberOfSectionand ?numberOfRowsInSection

return [arr count];

并尝试重新加载表格

 [table reloadData];
于 2012-04-04T10:24:33.727 回答
1

我遇到了同样的问题,但意识到将数据添加到列表中更简单,然后重新加载表,如下所示:

    [self.dataForList addObject:newRow];
    [self.unsignedTableView reloadData];

然后,委托方法接管以返回正确的行数:

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [self.unsignedTableView count];
     }
于 2012-06-19T19:55:30.040 回答
1

您的问题的原因是您在两个不同的点更改数据源和用户界面。您对数据源的更新和插入调用都必须在同一个开始/结束块中。

因此,只需将 beginUpdates 调用移到 addObject 调用前面即可解决您的问题。

于 2012-10-12T12:56:20.417 回答
0

这是一个想法

在事件调用时(当数组的内容发生变化时)将布尔值设置为 true [tableView reloadData]; 一个 in tableView:numberOfRowsInSection: 重置数字。

确保也相应地更改 tableView:cellForRowAtIndexPath:

于 2012-04-04T10:28:27.680 回答
0
if (indexpath) {
      [self.tableView beginUpdates];
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationMiddle];
      [self.tableView endUpdates];
}
于 2012-04-04T11:27:49.107 回答
0

我遇到了同样的问题并解决了我的问题,我没有初始化填充我的表的 NSMutableArray。我做的一切都是正确的,但忘记了重要的一点。

myArrayName = [[[NSMutableArray alloc] init]];

您的情况可能会有所不同,我建议您在尝试修改代码之前退一步查看您的代码。几个月后,但希望这会有所帮助!

于 2012-08-17T07:03:42.120 回答
0

一个老问题的答案..

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is zero now
                              inSection:0]];

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

交换这些,它应该可以工作:

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is also 1 now, hooray
                              inSection:0]];
于 2012-08-20T12:52:04.383 回答
0

当我遇到这个问题时,让我印象深刻的是我的 ReuseIdentifiers 不一样。对我来说,唯一的区别是大写字母。不要认为你的问题一开始是非常复杂的。仔细检查所有简单的事情。

如果您使用 Storyboard,您应该从界面构建器中的单元格属性中复制字符串并将其粘贴到 tableView 代码中的 ReuseIdentifier 中。

于 2014-02-14T20:04:22.337 回答