代码更新:在马修的回答之后,我尝试更正我的代码以使其更正确。现在代码确实删除了单元格,但也会崩溃并给出错误:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[__NSPlaceholderArray initWithObjects:count:]: 尝试从 objects[0] 插入 nil 对象”
checkboxTapped
下面的代码来自我的代码中调用的操作CustomCell
。一旦动作被触发,它就会给出错误。我发现 myindexPath
等于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:]: index 1 beyond bounds [0 .. 0]”
我的猜测是它与我有关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];
}