2014-05-13 42 views
0

我是iOS的初學者。我正在執行滑動刪除選項。我想在刪除行之前顯示一個警報視圖。我如何執行此操作。在iOS中滑動刪除時顯示提醒

- (void)tableView:(UITableView *)tableView commitEditingStyle: 
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSLog(@"%@",collisionsArray); 
if (editingStyle == UITableViewCellEditingStyleDelete) 
{  
    NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults]; 
    NSString *userId = [userinfo valueForKey:@"user_id"]; 
    if(userId!=nil) 
    { 
    NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section]; 
    collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]]; 
    NSLog(@"%@",collisionId); 
// removes saved datas from database 
    BOOL result = [database removeCollisionDetails:collisionId:@"accident_report"]; 
    if(result) 
    { 
    [[SHKActivityIndicator currentIndicator] 
    displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)]; 
    [self.navigationController popViewControllerAnimated:YES]; 
    } 
    else 
    { 
    [[SHKActivityIndicator currentIndicator] 
    displayCompleted:NSLocalizedString(@"val_error", nil)]; 
    } 
    } 
} 
[self.tableView reloadData]; 
} 

回答

2

爲此,您可以只顯示在ALET觀點:

if (editingStyle == UITableViewCellEditingStyleDelete){ 
// Show your alert view 
// Set its delegate to self 
} 

現在,你需要做的是這樣的:

#pragma mark ---- Delegate for alertview ---- 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults]; 
    NSString *userId = [userinfo valueForKey:@"user_id"]; 
    if(userId!=nil) 
    { 
     NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section]; 
     collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]]; 
     NSLog(@"%@",collisionId); 
// removes saved datas from database 
      BOOL result = [database removeCollisionDetails:collisionId:@"accident_report"]; 
     if(result) 
      { 
       [[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)]; 
       [self.navigationController popViewControllerAnimated:YES]; 
      } 



else 
     { 
      [[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_error", nil)]; 
     } 
     } 
    } 
+0

感謝您解決問題... – Chan

+0

如果問題解決,那麼請不要忘了查馬克的答案,這可能幫助其他用戶找到解決方案迅速:) – Ashutosh

0

使用UIAlertView如果用戶確認只能刪除。更新您的代碼:

if (editingStyle == UITableViewCellEditingStyleDelete) 
{  

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello World!" 
                 message:@"Are you sure ?" 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 

[alertView show]; 
} 

落實UIAlertViewDelegate和原刪除代碼移動到您檢測哪個按鈕被點擊的委託方法。

+0

看@Ashutosh答案委託實現部分 – giorashc

+0

謝謝..解決了問題 – Chan

1
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 


if (editingStyle == UITableViewCellEditingStyleDelete) { 
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warring" message:@"Are You Sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes ", nil]; 


[alert show]; 



} 


} 
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (buttonIndex == 0) { 


}else if (buttonIndex == 1){ 
    NSIndexPath *indexPath=[_UserTableView indexPathForSelectedRow]; 
    [showUData removeObjectAtIndex:indexPath.row]; //showUData NSMutableArray 
    [_UserTableView reloadData]; 
    [storeData setObject:showUData forKey:@"SendData"]; //storeData NSUserDefault 
    [storeData synchronize]; 
} 

} 
+0

考慮解釋一下你的代碼,說明它是如何工作的 –

+0

表格視圖單元執行刪除記錄......和單元格顯示刪除方法[UITableViewCellEditingStyleDelete]和自動顯示刪除按鈕 –