5

私はジェスチャーレコグナイザーを使用しています:

で初期化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.**
4

3 に答える 3

3

次のように tableView:willDisplayCell:forRowAtIndexPath: メソッドを提供して、UITableView の代わりに UITableViewCell に追加してみてください。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [cell addGestureRecognizer:longPressRecognizer];
}
于 2010-07-23T00:46:12.530 に答える
2

テーブルビューの代わりに、テーブルビューセルにジェスチャーレコグナイザーを追加してみてください。UITableViewCells は UIView から派生するため、ジェスチャ レコグナイザーを受け入れることができます。

于 2010-07-23T00:26:12.417 に答える
1

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;

}

次に、[[gestureRecognizer view] tag] を使用して longPress のそのタグにアクセスします (私のコードでは、myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]]; の部分です)。

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

}

于 2010-11-30T17:40:56.173 に答える