2015-06-27 4 views
0

Я пытаюсь открыть AlertView, даже если удерживаю строку tableview 0.5 секунды.TapGesture by UITableView

Я использую для этого следующего кода:

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

Этого код будет работать, но проблема в том, что я должен сначала щелкнуть строку, прежде чем она открывает предупредительный вид. Надеюсь, вы понимаете, что я имею в виду.

+0

Вы не должны использовать NSUserDefaults, как вы здесь. вместо этого присвойте 'cellText' iVar. – vikingosegundo

ответ

0

Почему Вы добавляете UILongPressGestureRecognizer к self.view, если вы хотите, чтобы вызвать при нажатии кнопки table..add UILongPressGestureRecognizer к вашему UITableView ([self.youTableViewName addGestureRecognizer:longTap]), и он будет работать нормально.

Для большего понимания того, как сделать it..check ниже ссылки ..

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

0

Я использовал ниже GestureRecognizer Кодекса, может быть полезным для вас.

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

} 
Смежные вопросы