Objective c 通过警报确认删除UITableViewController中的行
我有一个Objective c 通过警报确认删除UITableViewController中的行,objective-c,ios,Objective C,Ios,我有一个UITableViewController,设置为用户可以删除行。这是我的delete操作处理程序代码,以及UIAlertView的委托方法: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == U
UITableViewController
,设置为用户可以删除行。这是我的delete操作处理程序代码,以及UIAlertView
的委托方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"t" message:@"del?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
if (buttonIndex != [alertView cancelButtonIndex]) {
//my code to delete from the data source is here. it works fine.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationFade];
}
}
问题在于最后一行,它实际上应该从表视图
中删除行。如果我把它(以及从数据源删除的代码)放在第一个方法中,而不是放在UIAlertView
的东西中,它就可以正常工作
这样做的正确方法是什么?问题是
UIIndexPath
对象在该方法中是错误的!我猜这是因为在显示警报视图时没有选择行。因此,在执行UIAlertView
代码之前,我将其保存到视图控制器类的属性中,并在稍后的第二个方法中检索到它,它工作正常。问题在于UIIndexPath
对象在该方法中是错误的!我猜这是因为在显示警报视图时没有选择行。因此,在执行UIAlertView
代码之前,我将其保存到视图控制器类的属性中,并在稍后的第二个方法中检索到它,它工作正常