5

Tôi đang sử dụng recognizers cử chỉ:Làm cách nào để xác định liệu người dùng đã nhấn vào UITableViewCell trong 2 giây?

Initialize trong viewDidLoad:

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

Đây là những gì longPress trông giống như:

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

Làm thế nào tôi có thể buộc này vào?

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

Làm cách nào đểSelectRowAtIndexPath tham chiếu đến gestureRecognizer.minimumPressDuration?

Về cơ bản những gì tôi đang cố gắng để đạt được là:

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

Trả lời

3

Hãy thử thêm nó vào UITableViewCell thay vì UITableView bằng cách cung cấp các tableView: willDisplayCell: forRowAtIndexPath: Phương pháp như vậy:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell addGestureRecognizer:longPressRecognizer]; 
}
2

Bạn có thể thử thêm gesturerecognizer đến tableviewcell, thay vì tableview. UITableViewCells có nguồn gốc từ UIView, vì vậy họ có thể chấp nhận các trình nhận dạng cử chỉ.

+0

bạn có thể chỉ cho tôi trong mã? –

+1

Steve dường như đã làm điều đó. –

1

thêm các indexpath.row vào thẻ của 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; 

}

và sau đó truy cập thẻ đó trong longPress bằng cách sử dụng thẻ [[gestureRecognizer view]] (trong mã của tôi một phần với myModalViewController.previousObject = [_currentItemArray objectAtIndex: [[người gửi xem] 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]; 
} 

}

Các vấn đề liên quan