0

I try to open an AlertView even I hold the tableview row 0.5 seconds.

I use for that following code:

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

UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[longTap setMinimumPressDuration:0.5];
longTap.delegate = (id)self;
[self.view addGestureRecognizer:longTap];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;

[[NSUserDefaults standardUserDefaults] setObject:cellText forKey:@"CellNameToEdit"];

}

- (void)handleTapGesture:(UILongPressGestureRecognizer *)sender{

if (sender.state == UIGestureRecognizerStateBegan) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Titel" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];

    alert.tag = 1;

    [alert show];
}
}

This code will work, but the problem is, I have to click the row first before it opens the alert view. I hope you understand what I mean.

hantoren
  • 357
  • 2
  • 17

2 Answers2

0

Why are you adding UILongPressGestureRecognizer to self.view if you want it to get called when you click table..add UILongPressGestureRecognizer to your UITableView([self.youTableViewName addGestureRecognizer:longTap]) and it will work fine.

For more insight of how to do it..check out below link..

https://stackoverflow.com/a/3924965/1865424

Community
  • 1
  • 1
Kundan
  • 3,084
  • 2
  • 28
  • 65
0

I have used below GestureRecognizer Code, may be Helpful to You.

#pragma mark - View Controller Life Cycle
@implementation ViewController <UIGestureRecognizerDelegate>

- (void)viewDidLoad
{
    UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
    [gesture1 setDelegate:self];
    [gesture1 setMinimumPressDuration:1];
    Tableview.userInteractionEnabled = YES;
    [Tableview addGestureRecognizer:recognizer];
}

#pragma mark - DidSelcelect kindof method
-(void)gestureAction:(UITapGestureRecognizer *) sender
{

    CGPoint touchLocation = [sender locationOfTouch:0 inView:self.Tableview];

    //here is indexpath
    NSIndexPath *indexPath = [self.Tableview indexPathForRowAtPoint:touchLocation];

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

    //Do here what you want to do with Cell
    [self.Tableview selectRowAtIndexPath:indexPath
                                animated:YES
                          scrollPosition:UITableViewScrollPositionNone];

}
Mehul
  • 3,033
  • 23
  • 37