我的UITableViewController
和NSIndexPath
一起工作时遇到了一个奇怪的问题。在我的UITableView
中,我实现了一个自定义的OptionCell,它在点击的单元格下方淡入淡出,然后淡出。使用iOs 4.3,它可以完美运行。
我最近更新了新的xCode和iOs 6.我更改了项目中的相关点,除了每次尝试删除customCell时出现的崩溃,一切正常。
它崩溃了:
- [NSIndexPath row]
中的断言失败[...]
***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'索引路径无效 与UITableView一起使用。传递给表视图的索引路径必须包含 恰好有两个索引指定了section和row。请使用 如果可能,在UITableView.h中的NSIndexPath上的类别。
我对这个错误进行了一些研究。我找到this question,检查了上述步骤,但没有帮助。
调试时我发现每次检查row
的{{1}}时都会崩溃。它不是NIL而应该有一个(适用于旧的iOs版本)。他们是否在版本之间使用NSIndexPath更改了某些内容?我已经检查了一切我能想到的......
我在以下代码段中标记了错误:
indexPathToDelete
编辑: modelIndexPathforIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
/* if user tapped the same row twice get rid of the OptionCell */
if([indexPath isEqual:self.tappedIndexPath]){
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/* update the indexpath if needed...*/
indexPath = [self modelIndexPathforIndexPath:indexPath];
/* pointer to delete the control cell */
NSIndexPath *indexPathToDelete = self.controlRowIndexPath;
/* if same row is tapped twice => clear tapping trackers */
if([indexPath isEqual:self.tappedIndexPath]){
self.tappedIndexPath = nil;
self.controlRowIndexPath = nil;
}
/* otherwise update them appropriately */
else{
self.tappedIndexPath = indexPath; /* the row the user just tapped. */
/* Now set the location of where I need to add the option cell */
self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
}
/* all logic is done, lets start updating the table */
[tableView beginUpdates];
/* lets delete the control cell, either the user tapped the same row twice or tapped another row */
if(indexPathToDelete){
/* CRASH APPEARS HERE!: */
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
withRowAnimation:UITableViewRowAnimationRight];
}
/* lets add the new control cell in the right place */
if(self.controlRowIndexPath){
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath]
withRowAnimation:UITableViewRowAnimationLeft];
// [self.tableView scrollToRowAtIndexPath:self.controlRowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
/* and we are done... */
[tableView endUpdates];
} else {
if (self.controlRowIndexPath) {
indexPath = [self modelIndexPathforIndexPath:indexPath];
NSIndexPath *indexPathToDelete = self.controlRowIndexPath;
self.tappedIndexPath = nil;
self.controlRowIndexPath = nil;
if(indexPathToDelete){
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
withRowAnimation:UITableViewRowAnimationFade];
}
}
VerwaltungViewController* verwaltungViewController;
verwaltungViewController = [[VerwaltungViewController alloc] init];
[self.navigationController pushViewController:verwaltungViewController animated:YES];
}
}