如何在 alertview 上找到索引路径?

How to find the indexpath on alertview?

您好,我正在使用 tableview.Now 我遇到了一个问题。在我的表格视图中,当我使用长手势时,我需要显示警报 View.it 正在工作 fine.when 我在警报视图中单击按钮索引 0 我需要执行一些 Task.But 因为我需要索引路径。下面是我执行任务的方法

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(messageDeleteOrForword:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.tableView addGestureRecognizer:lpgr];

}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {
  [self deleteSpecificMessage];

}
if (buttonIndex==1) {

}
}

 -(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
 {
   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
[alert show];
}


-(void)deleteSpecificMessage
{

 CGPoint p = [gestureRecognizer locationInView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
}

但我需要传递一些参数来了解我在下面使用的索引路径 -(void)deleteSpecificMessage:(id)发件人 { } 但是如何在alertview中调用和分配参数请帮助我。

在您的删除方法中,您可以添加:

CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];

显示来自 didselectrowatindexpath 的警报视图。从此方法将 alertview 标记设置为索引路径。通过这种方式,您会将 alertview 与 indexpath 集成。在 alertview delegate 中,你可以获得 indexpath 作为它的标签。所以你可以使用这个标签来删除它或转发它作为考虑索引路径。

例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];

 alert.tag = indexPath;  //setting tag

 [alert show];
}

更新(如评论中所述):

你可以这样做,这里有一个例子,

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];

这里handleLongpress方法

 -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
 CGPoint p = [gestureRecognizer locationInView:self.myTableView];

NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
    NSLog(@"long press on table view but not on a row");
}
else{

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];

    alert.tag = indexPath;  //setting tag

    [alert show];
}

}

希望这会有所帮助:)

您可以使用

获取索引路径
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

您可以从this link

查看更多详情

您还可以在 .h 文件中获取一个 IndexPath 变量,该变量可以在选择表格视图单元格时分配。

首先添加长按手势识别器。

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionLongPressGeature:)];
[longPressGesture setDelegate:self];
[longPressGesture setMinimumPressDuration:0.3];
[tableView addGestureRecognizer:longPressGesture];

选项 1

-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:tableViewJobs];
   // You need to declare NSIndexPath *indexPathSelected in your .h File
    indexPath = [tableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
            // Perform your action i.e AlertView
    }
}

选项 2:

-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
        // You need to declare NSIndexPath *indexPathSelected in your .h File
    indexPath = [tableView indexPathForSelectedRow];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        // Perform your action i.e AlertView
    }
}

以后您可以使用这个变量。

希望这就是您要找的东西。

-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
 {   
     CGPoint p = [gestureRecognizer locationInView:self.myTableView];

     NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
     if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
     }
     else{

        NSLog(@"selected row index %ld",(long)indexPath.row);

        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
        [alert show];
     } 


 }

您还可以使用 Objective-C 关联对象作为运行时属性。此功能在 <objc/runtime.h> 中可用。例如:

#import <objc/runtime.h>

-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
   objc_setAssociatedObject(alert, @"currentIndexPath", indexPath, OBJC_ASSOCIATION_RETAIN);
   [alert show];
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
   NSIndexPath *idxPath = objc_getAssociatedObject(alertView, @"currentIndexPath");
   NSLog(@"%@",idxPath);

   if (buttonIndex == 0) {
      [self deleteSpecificMessage];
   }

   if (buttonIndex==1) {
   }
}

这里有一些 link 更有帮助。 http://kingscocoa.com/tutorials/associated-objects/ http://nshipster.com/associated-objects/

使用关联对象,您可以将多个属性附加到任何对象并轻松获得。在第一个 link 中,您还可以通过定义类别来自定义属性。