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: 方法将其添加到 UITableViewCell 而不是 UITableView:

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

您可以尝试将手势识别器添加到 tableviewcell,而不是 tableview。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 回答