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

向 tableView 控制器类添加一个变量来保存 indexPath.row,然后在 alertview 中使用它。在显示警报之前将 indexPath.row 保存在其中。

你也需要打电话

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

在确认用户在 alertView 委托方法中选择了 YES 之后。

于 2014-09-18T11:51:08.427 回答
1

如果要传递的信息是一个集合对象,您也可以使用 objc_setAssociatedObject。链接 - 如何将变量传递给 UIAlertView 委托?

于 2014-09-18T12:01:56.333 回答