长按(触摸并等待)tableView中的单元格
我有一个tableview,我想当我触摸单元格导航到editViewController,当我长触摸(触摸和等待)单元格导航到DetailsViewController
1)添加一个UILongPressGestureRecognizer
给你的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; //add longPressGestureRecognizer to your cell UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; //how long the press is for in seconds lpgr.minimumPressDuration = 1.0; //seconds [cell addGestureRecognizer:lpgr]; } return cell; }
2)处理editViewController
并推送给你editViewController
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; if (indexPath == nil) { NSLog(@"long press on table view but not on a row"); } else { if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"long press on table view at row %ld", (long)indexPath.row); editViewController *editView = [self.storyboard instantiateViewControllerWithIdentifier:@"editView"]; //dont forget to set storyboard ID of you editViewController in storyboard [self.navigationController pushViewController:editView animated:YES]; } } }
3)正常按下您的DetailsViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailsViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"]; //dont forget to set storyboard ID of you editViewController in storyboard [self.navigationController pushViewController:detailView animated:YES]; }