1

アラート ビューを使用して、ユーザーからの確認後にテーブル ビューで行を削除しようとしています。UIAlertViewDelegateただし、テーブル内のどの行を削除するかをメソッドに知らせる方法がわかりません。

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert_delete = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"Confirm Delete %@",[names objectAtIndex:indexPath.row] ] message:@"Warning all student data will be earsed" delegate:self cancelButtonTitle:@"Dismess" otherButtonTitles:@"YES", nil];
        [alert_delete show];

        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

アラートメソッドで、テーブルとデータベースから行を削除するためにそれを処理しようとします

    -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
        if ([title isEqualToString:@"YES"]) {
           // how to pass indexPath.row to alertview 
             [names removeObjectAtIndex:indexPath.row];
        }


    }
4

4 に答える 4

2

デリゲートに何かを渡したい場合は、デリゲート クラスにプロパティを追加し、アラート ビューを呼び出す前に情報を渡します。

@interface AlertDelegate : NSObject <UIAlertViewDelegate>
@property (nonatomic) NSIndexPath *indexPath;
@end

// @implementation AlertDelegate omitted

そして、次のように使用します。

UIAlertView *alertView = ...;
AlertDelegate *delegate = [AlertDelegate new];
alertView.delegate = delegate;
delegate.indexPath = indexPathFromSomewhere;
[alertView show];   // or whatever

デリゲートが の場合self、プロパティを追加するself(またはプライベート インスタンス変数を使用する) ことを意味します。

デリゲート メソッドでは、indexPath にアクセスできます。

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
    if ([title isEqualToString:@"YES"]) {
        [names removeObjectAtIndex:self.indexPath.row];
    }
}
于 2014-09-18T11:45:14.053 に答える
1

代わりに UIAlertview を使用 CustomAlertview CustomAlertView *lAlert = [[CustomAlertView alloc] init.....

#import <UIKit/UIKit.h>

@interface CustomAlertView : UIAlertView
@property (nonatomic,retain)NSString *mIndex;
@end
#import "CustomAlertView.h"

@implementation CustomAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

デリゲート メソッドでは、以下のように選択したデータまたはインデックスを取得します

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSLog(@"index %@",((CustomAlertView *)alertView).mIndex);
    }

In the table delegate method assing the index or data as below 
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
 CustomAlertView *lAlert = [[CustomAlertView alloc] .....
lAlert.mIndex = @"1";
  [lAlert show];
     lAlert = nil;
 }
于 2014-09-18T12:24:15.763 に答える
1

渡す情報がコレクション オブジェクトの場合は、objc_setAssociatedObject を使用することもできます。link - 変数を UIAlertView デリゲートに渡すにはどうすればよいですか?

于 2014-09-18T12:01:56.333 に答える
1

tableView コントローラー クラスに変数を追加して indexPath.row を保持し、それを alertview で使用します。アラートを表示する前に indexPath.row を保存します。

また、電話する必要があります

 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

ユーザーがalertViewデリゲートメソッドでYESを選択したことを確認した後。

于 2014-09-18T11:51:08.427 に答える