在 uitableview 的同一部分插入/创建新行
我遇到了一个问题,我有一个 uitableview,行数为 5。 如果用户选择一行,则应在点击的行正下方创建/插入一个新行并带有动画(正如我们在隐藏/取消隐藏部分中看到的那样),并且在点击新插入的行时应将其删除。
我已经尝试过,但它说
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 (6) must be equal to the number of rows contained in that section before the update
(5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
那么实现此功能的其他方法应该是什么? 提前致谢。
I am stuck in a problem, I have a uitableview with rows let say 5.
If a user selects one row then a new row exactly beneath the tapped row should be created/inserted with an animation (as we have seen with sections hiding/unhiding) and upon tap of the newly inserted row it should be deleted.
I have given a try but it says
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 (6) must be equal to the number of rows contained in that section before the update
(5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
so what should be the other way to achieve this functionality ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最初您有 5 行。您向表中添加一个新行,假设使用 addRowsAtIndexPaths: 方法。此时,您的表视图将调用其数据源方法,因为它需要添加这个新单元格。
但是,您的数据源方法可能仍然返回 5 行(而不是 6 行),这导致了不一致(因为表视图需要 6 行,而您仍然返回 5 行)
所以,假设当表视图调用时cellForRowAtIndexPath:新创建的单元格(行= 5)的方法,它可能会崩溃,因为您必须执行以下操作:
上述语句将导致崩溃,因为indexPath.row是5并且您的数组中仍然有5个对象(索引 0 到 4)。因此 objectAtIndex:5 会导致崩溃。
Initially you have 5 rows. You add a new row to the table, lets say using addRowsAtIndexPaths: method. At this point, your table view will call its datasource methods, as it needs to add this new cell.
But, probably your still returning number of rows as 5 (and not 6), from your datasource methods, which resulted in inconsistency (as table view expects 6 rows and you are still returning 5 rows)
So, lets say when the table view calls cellForRowAtIndexPath: method for the newly created cell (row = 5), it could crash because you must be doing something as:
The above statement would result in crash, as indexPath.row is 5 and your array still has 5 objects in it (index 0 to 4). Thus objectAtIndex:5 results in crash.
我犯了两个错误
1)更新后我没有使用[tableView beginUpdates]并且显然使用[tableView endUpdates]
2) 计算 newRow 的索引路径的方法不明确。
非常感谢 pratikshabhisikar 和 Max Howell 投入的时间和精力。
I was making 2 mistakes
1) I wasnot using [tableView beginUpdates] and obviously [tableView endUpdates] after updation
2) The method to calculate the indexpath for a newRow was ambigious.
Thankyou very much pratikshabhisikar and Max Howell for putting your time and efforts in.