长按uiTableView IOS更改单元格的select

在uitableview上正常的单元格select工作正常。 但是,当长按事件被称为一个单元格时,它会再次select先前select的单元格。 例如,如果用户select第一个单元,然后长按第二个单元,则为第二个单元调用长按事件,但是select再次返回到第一个单元。

这是我的代码:

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

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

看起来,你遇到的问题是根植于这样一个事实,即将一个单元格标记为选中状态并在该单元格上处理长按手势事件是分开的。 我对你的问题的解释是,你有一个单一的select(而不是多个)的表格视图,并希望单元格成为“选中”,通过正常的水龙头select行动,并通过识别长按手势。 然而,当你想让单元格被长按手势选中时,你会希望longpress导致与通过正常的点击select不同的动作(例如,如果用户点击了一个你想要启动的视图控制器A但是如果用户长按这个单元格,你想启动视图控制器B,并在这两种情况下,你希望该表将单元格视为“选中)…让我知道你是否想要的是不同于这个,我可以更新答案。

这对表格视图来说不是一个很常见的行为,但是我们可以通过修改你的代码来使它工作:

首先定义一个属性来跟踪长按是否正在发生:

@property (assign, nonatomic) BOOL longPressActive;

然后在你的handleLongPress方法中,告诉表格select该行:

 -(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); self.longPressActive = YES; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; }else if (gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateCancelled) { self.longPressActive = NO; } } 

最后,在表格中查看委托方法,在select后定义你期​​望的行为。 请注意,在例子中,长按任何单元格将导致相同的视图控制器显示。 为了以不同的方式设置视图控制器,您可以按照上一个问题中类似于我的答案的stream程进行操作,也可以在实例化后将行特定的数据传递给editViewController。

 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (self.longPressActive) { //Perform action desired when cell is long pressed editViewController *editView = [self.storyboard instantiateViewControllerWithIdentifier:@"editView"]; [self.navigationController pushViewController:editView animated:YES]; }else { //Perform action desired when cell is selected normally //Your code here } } 

希望这是有帮助的。