2

ユーザーが編集モードでデフォルトのdeleteボタンをタップすると、アラート ビューが表示され、アラート ビューでもう一度 [削除] をクリックすると、行が削除されます。UITableView

  [[self categoriesArray]removeObjectAtIndex:[indexPath row]];
   NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
   [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
   [self.categoriesArray writeToFile:[self dataFilePath]  atomically:YES];

しかし今、alertviewデリゲートメソッドで同じコードを使用する必要があるため、取得方法がわかりません[indexPath row]

4

4 に答える 4

4

インデックスパスをタグに設定し、UIAlertViewデリゲートから取得します。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
       UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Are you sure want to delete" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
       [alertView setTag:indexPath.row]; // Assigning here.
        [alertView show];
    }
}
    // UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d",alertView.tag); // Your Indexpath is here

**Edited:**

    NSIndexPath * path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
    [[self categoriesArray]removeObjectAtIndex:[path row]];
    NSArray * indexPathsToRemove = [NSArray arrayWithObject:path];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
    [self.categoriesArray writeToFile:[self dataFilePath]  atomically:YES];
}
于 2013-04-05T12:50:39.400 に答える
2

commitEditingStyle メソッドのビューを警告するタグを次のように設定します。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Delete Record" message:@"Are you sure to delete the record." delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
       alert.tag = indexPath.row;
       [alert show];
    }
}

オンAlert delegateメソッド

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     if(buttonIndex == 1)//YES button clicked
     {
         [[self categoriesArray]removeObjectAtIndex:alertView.tag];
     }
}
于 2013-04-05T12:49:26.973 に答える
1

UITableViewこの情報は、次の方法で利用できます。

- (NSIndexPath *)indexPathForSelectedRow

または、複数の選択が行われた場合:

- (NSArray *)indexPathsForSelectedRows

于 2013-04-05T12:47:03.473 に答える
1

このデリゲート メソッド内でアラート ビューを使用する

  - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

ユーザーが削除をクリックした場合にやりたいことは何でもします。ユーザーがアラートビューで削除を押した場合、アラートビューデリゲートメソッドを使用して処理する必要があります。

于 2013-04-05T12:47:45.487 に答える