Как я могу определить, нажимал ли пользователь на UITableViewCell в течение 2 секунд? - PullRequest
5 голосов
/ 23 июля 2010

Я использую распознаватели жестов:

Инициализировать в viewDidLoad:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
 [self.view addGestureRecognizer:longPressRecognizer];

Вот как longPress выглядит:

- (void)longPress:(UILongPressGestureRecognizer*)gestureRecognizer {
 if (gestureRecognizer.minimumPressDuration == 2.0) {
  NSLog(@"Pressed for 2 seconds!");
 }
}

Как можноЯ связываю это с?

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

Каким образом didSelectRowAtIndexPath получит ссылку на gestureRecognizer.minimumPressDuration?

По сути, я пытаюсь достичь:

**If a user clicks on a cell, check to see if the press is 2 seconds.**

Ответы [ 3 ]

3 голосов
/ 23 июля 2010

Попробуйте добавить его в UITableViewCell вместо UITableView, предоставив tableView: willDisplayCell: forRowAtIndexPath: метод, подобный так:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [cell addGestureRecognizer:longPressRecognizer];
}
2 голосов
/ 23 июля 2010

Вы можете попробовать добавить распознаватель жестов в ячейку просмотра таблицы вместо просмотра таблицы. UITableViewCells являются производными от UIView, поэтому они могут принимать распознаватели жестов.

1 голос
/ 30 ноября 2010

добавьте indexpath.row к тегу tableviewcell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(rowButtonAction:)];
[cell setTag:indexPath.row];
[cell addGestureRecognizer:longPressRecognizer]; 
[longPressRecognizer release];

// Configure the cell...
Group *gp = [_currentItemArray objectAtIndex:indexPath.row];
cell.textLabel.text = gp.name;


return cell;

}

, а затем получите доступ к этому тегу в longPress, используя тег [[gestRecognizer view]] (в моем кодеэто часть с myModalViewController.previousObject = [_currentItemArray objectAtIndex: [[представление отправителя]]];

-(IBAction)rowButtonAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded) {
    GroupsAdd_EditViewController *myModalViewController = [[[GroupsAdd_EditViewController alloc] initWithNibName:@"GroupsAdd_EditViewController" bundle:nil] autorelease];
    myModalViewController.titleText = @"Edit Group";
    myModalViewController.context = self.context;
    myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]];
    [self.navigationController presentModalViewController:myModalViewController animated:YES];
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...