我有UITableView
从CoreData
加载数据。如果我选择从表中删除记录,我使用:
[self.managedObjectContext deleteObject:[self.recordList objectAtIndex:indexPath.row]];
[self.managedObjectContext save:nil];
[self.tableViewSavedRecords reloadData];
一切都很好 - 删除记录并重新加载表格。
但是有些记录受密码保护,当选择删除这样的记录时,会出现警告要求输入密码:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Warning" message: @"This record has been password protected.\nEnter password." delegate: self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
alertView clickedButtonAtIndex
:
NSString *enteredPassword = [[alertView textFieldAtIndex:0] text];
if ([enteredPassword isEqualToString: savedPassword]) {
[self.managedObjectContext deleteObject:[self.recordList objectAtIndex:indexPath.row]];
[self.managedObjectContext save:nil];
[self.tableViewSavedRecords reloadData];
}
问题objectAtIndex:indexPath.row
显然无法在alertView clickedButtonAtIndex
部分识别出来。如何将NSIndexPath
传递给alertView clickedButtonAtIndex
?
我试过声明:
NSIndexPath *selectedIndexPath;
然后,如果警报被触发,我已经包括:
selectedIndexPath = [tableViewPausedObs indexPathForSelectedRow];
然后在AlertView中单击:
[self.managedObjectContext deleteObject: selectedIndexPath.row];
[self.managedObjectContext save:nil];
但它会删除表格的第一行,而不是选定的行。
答案 0 :(得分:1)
你可以在任何地方获得所选行的NSIndexPath
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];