我正在尝试使用警报视图在用户确认后删除表视图中的行。但是我不知道如何让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
}
}
在alert方法中我尝试处理它以从表和数据库中删除行
-(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];
}
}
答案 0 :(得分: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];
}
}
答案 1 :(得分:1)
向tableView控制器类添加一个变量以保存indexPath.row,然后在alertview中使用它。在显示警报之前将indexPath.row保存在其中。
您还需要致电
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
确认用户在alertView委托方法中选择了YES。
答案 2 :(得分:1)
如果要传递的信息是集合对象,也可以使用objc_setAssociatedObject。链接 - How do you pass a variable to the UIAlertView delegate?
答案 3 :(得分: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;
}