Жест LongPress не вызывается в UITableView - PullRequest
0 голосов
/ 01 июня 2018

В приложении для iOS у меня есть UITableView (НЕ является частью UITableViewController), на котором я хочу обнаружить долгое нажатие на пользовательских UITableViewCells следующим методом:

- (void)viewDidLoad
{
    myTableView.delegate=self;
    myTableView.dataSource=self;

    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=0.5f;
    longTap.delegate=self;
    [myTableView addGestureRecognizer:longTap];
    [super viewDidLoad];
}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}

Однако longTapGestureCaptured никогда не вызываетсякогда я давлюКак это можно решить?

Ответы [ 2 ]

0 голосов
/ 03 июня 2018

Вы добавляете longPress даже для UITableview.Но когда у таблицы есть ячейки, ячейка таблицы будет перезаписана в UITableView, поэтому вы не можете долго нажимать на TableView.Reslove для вас это добавить longPress даже в каждой ячейке таблицы.Путем добавления кода добавьте longPress даже для каждой ячейки в функции cellAtIndex.Удачи.

0 голосов
/ 02 июня 2018

Я попробовал твой код.это на 100% работает для меня.Но небольшие изменения в вашем коде ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create cell here
    UITableViewCell *cell;
    cell= (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    //Add gesture to cell here
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=1.5f;
    longTap.delegate=self;
    [cell addGestureRecognizer:longTap];

    cell.textLabel.text = @"Name";//Send your data here

    return cell;

}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}
...