如何在 alertview 上找到 indexpath?

Posted

技术标签:

【中文标题】如何在 alertview 上找到 indexpath?【英文标题】:How to find the indexpath on alertview? 【发布时间】:2016-04-21 08:01:50 【问题描述】:

您好,我正在使用 tableview。现在我面临一个问题。在我的表格视图中,当我使用长手势时,我需要显示警报视图。它工作正常。当我在警报视图中单击按钮索引 0 时,我需要执行一些任务。但我需要索引路径。以下是我执行任务的方法

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)sender 但是如何在alertview中调用和分配参数请帮助我。

【问题讨论】:

请贴一些长手势的代码。 你能检查更新的吗 【参考方案1】:

显示来自didselectrowatindexpath 的警报视图。从此方法将 alertview 标记设置为 indexpath。通过这种方式,您将您的警报视图与 indexpath 集成。在 alertview 委托中,您可以获得 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];



希望这会有所帮助:)

【讨论】:

CGPoint p = [gestureRecognizer locationInView:self.myTableView];您将从 tableview 获得 CGPoint,现在您可以根据行高计算索引,并且应该长按。【参考方案2】:

您可以使用

获取索引路径
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
    

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

我希望这就是你想要的。

【讨论】:

请接受我的问题,它已被其他人使用,谢谢【参考方案3】:

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

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

【讨论】:

但是gestureRecognizer呢?【参考方案4】:
-(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];
      


 

【讨论】:

【参考方案5】:

您还可以将 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) 
   

这里有一些更有用的链接。 http://kingscocoa.com/tutorials/associated-objects/http://nshipster.com/associated-objects/

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

【讨论】:

以上是关于如何在 alertview 上找到 indexpath?的主要内容,如果未能解决你的问题,请参考以下文章

在alertview中选择取消按钮时如何从数组中关闭所有alertview?

如何在 alertview 中返回 viewcontroller

alertView:(UIAlertView *) alertView 方法在 Appdelegate 中不起作用

iOS-如何在代码开始之前显示 alertView

如何在不同的行中添加 alertView 中的按钮?

如何将另一个 ViewController 设置为 AlertView 的委托