2017-04-11 57 views
0

我有一個UITableView加載數據從CoreData。如果我選擇刪除表中的記錄,我使用:傳遞NSIndexPath到alertView clickedButtonAtIndex

[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部分未被識別。我如何通過NSIndexPathalertView clickedButtonAtIndex

我曾嘗試聲明:

NSIndexPath *selectedIndexPath; 

然後,如果觸發警報我已經包括:

selectedIndexPath = [tableViewPausedObs indexPathForSelectedRow]; 

然後在AlertView點:

[self.managedObjectContext deleteObject: selectedIndexPath.row]; 
[self.managedObjectContext save:nil]; 

但它刪除表的第一行,而不是選定的行。

+0

隨時隨地獲取選定行的NSIndexPath'UIAlertView'已經過時了幾年。使用基於塊的'UIAlertController',並且可以捕獲索引路徑。 – vadian

回答

1

你可以這樣

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 
+0

謝謝。我試過這個,但我得到警告不兼容的指針類型發送'NSIndexPath * _Nullable'到類型'NSManagedObject * _Nonnull'+的參數。當我運行代碼NSIndexParth是零和崩潰。查看更新。 – RGriffiths

+0

你改變了這個'[self.managedObjectContext deleteObject:[self.recordList objectAtIndex:selectedIndexPath.row]];'? –

+0

再次,謝謝。我沒有添加.row,它擺脫了警告。但是,它不會選取所選行並每次刪除第一行。 – RGriffiths

相關問題