[英]NSInvalidArgumentException when deleting cell using a different class
代码更新:根据Matthew的回答,我尝试更正我的代码以使其更正确。 现在,代码确实删除了单元格,但也崩溃并给出了错误:
*由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ * -[__ NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]中插入零对象”
以下代码来自我的CustomCell
代码中一个名为checkboxTapped
的操作。 一旦动作被触发,就会产生错误。 我发现我的indexPath
等于NULL
,这很可能是问题所在。 但是我不知道如何解决。
[self.textLabel setTextColor:[UIColor grayColor]];
[self.detailTextLabel setTextColor:[UIColor grayColor]];
parent = [[ViewController alloc] init];
db = [[DataObject alloc] init];
NSIndexPath *indexPath = [[parent tableView] indexPathForSelectedRow];
[[parent array] removeObjectAtIndex:[indexPath row]];
[db deleteTaskAtIndex:[indexPath row]];
[[parent tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[db release];
[parent release];
旧的:我查看了我的代码,并打印了我正在使用的数组,这看起来很好,但是此错误仍然存在。
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[__ NSArrayM removeObjectAtIndex:]:索引1超出范围[0 .. 0]'
我的猜测是它与indexPath
但是我indexPath
进行的更改没有多大区别。
-(void)checkboxTapped:(id)sender
{
[sender setSelected:YES];
[self.textLabel setTextColor:[UIColor grayColor]];
[self.detailTextLabel setTextColor:[UIColor grayColor]];
parent = [[ViewController alloc] init];
UITableView *tableView = parent.tableView;
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:parent.array];
[parent release];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[array count] inSection:1];
[array removeObjectAtIndex:[indexPath row]];
[db deleteTaskAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[array release];
[tableView endUpdates];
[tableView reloadData];
}
在您的代码中,[indexPath row]将返回[array count]的值。 那不可能是您想要的。 如果您的数组中有零个对象,则将尝试删除索引为0的对象。但是将没有对象,并且会出现错误。 如果数组中有1个对象,则将尝试删除索引1处的对象。同样,这将失败,因为索引1处没有对象,索引0处只有一个对象。
如果要删除数组中的最后一个对象,则需要使用count-1的索引。 您可能还需要检查数组是否为空,如果可能会发生这种情况。
更新以回应评论中的跟进
您不想做任何indexPathWithIndex
。 作为第一步 ,尝试大意如下修改代码:
-(void)checkboxTapped:(id)sender
{
[sender setSelected:YES];
[self.textLabel setTextColor:[UIColor grayColor]];
[self.detailTextLabel setTextColor:[UIColor grayColor]];
parent = [[ViewController alloc] init]; // looks very odd - is an instance of this viewController active when the checkBox is tapped? If so, you don't want to create a new one, you want to access the existing one
UITableView *tableView = parent.tableView;
[parent release]; // this looks very dicey - when you release the parent, won't it release the tableView too?!
int lastRow = [array count] - 1;
if (lastRow == 0)
{
return; // bail if there are no rows in the table
}
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:parent.array];
[array removeObjectAtIndex: lastRow]; // not clear this will do anything as the reference to array is discarded later
[db deleteTaskAtIndex: lastRow];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: lastRow inSection:1];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[array release];
// [tableView endUpdates]; // there's no matching beginUpdates and you're only do one change operation anyway - leave this out
// [tableView reloadData]; // if you leave this line in, you won't see the delete animation - if you just want to delete one row, you wouldn't normally use reloadData, at least not if you want the animation
}
说了这么多,看来这里还有其他事情。
array
发生了什么? 您创建它,从中删除一个项目并丢弃指向它的指针。 那是您真正想要做的。 一种更常见的模式是从另一个对象获取指向数组的指针,并在此处删除其末尾的项目。
从代码中还不清楚如何更新表的数据源。 使用deleteRowsAtIndexPaths:withRownAnimation
您需要确保表的数据源返回的行数比上次使用tableView:numberOfRowsInSection:
询问时返回的行少。 从您的代码中,还不清楚tableView dataSource如何知道少了一个项目,除非,也许正在寻找db
指向的内容以找出答案。
从根本上讲,使用典型的设计模式,当您释放父视图时,tableView将被释放,因此在[parentparent]之后,它指向的所有内容都将做未定义的操作,并且可能会使至少一些崩溃。时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.