2016-04-21 17 views
1

嗨,我正在與tableview.Now我正面臨着一個問題。在我的tableview中,當我使用長手勢我需要顯示Alert Alert.it工作正常。當我點擊alertin視圖中的buttonindex 0時,我需要執行一些Task.But,因爲我需要indexpath。下面是我的方法執行任務如何在alertview上找到indexpath?

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]; 
} 

,但我需要通過一些參數就知道了,我使用下面一行 的indexpath - (空)deleteSpecificMessage:(ID)發送 { } 但如何在alertview中調用和分配參數請幫助我。從didselectrowatindexpath

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

請發佈一些長手勢代碼。 –

+0

可以請你檢查更新的 – Bittoo

回答

0

你可以使用indexpath

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 

您可以從this link

同時檢查更多的細節,你可以在.h文件中一個IndexPath變量可以是分配時選擇tableview單元格。

首先添加您的長按手勢識別器。

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 
    } 
} 

和以後你可以使用這個變量。

我希望這是你在找什麼。

+1

請接受我的問題,這是其他人使用完整謝謝 – Bittoo

0

在你的刪除方法,你可以添加此。將alertview標記設置爲此方法的索引路徑。通過這種方式,你將把你的alertview與indexpath結合起來。在alertview委託中,您可以獲取indexpath作爲標記。所以你可以使用這個標籤刪除它或者轉發它作爲考慮的索引路徑。

forexample:

- (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]; 
} 

} 

希望這將有助於:)

+0

但是gestureRecognizer呢? – Bittoo

1

顯示警報視圖:

+0

好的,但我需要當我在那種情況下使用長按? – Bittoo

+0

檢查我的更新回答 – Lion

+0

CGPoint p = [gestureRecognizer locationInView:self.myTableView]; 您將從tableview中獲得CGPoint,現在按行高可以計算索引以及哪些應該已經很長時間。 – Hasya

0
-(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]; 
    } 


} 
0

您還可以使用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/

使用關聯對象可以將多個屬性附加到任何對象並輕鬆獲取。在第一個鏈接中,您還可以通過定義類別來定製屬性。