更改长按uiTableView IOS上的细胞选择

时间:2014-08-28 19:45:23

标签: ios objective-c uitableview

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

这是我的代码:

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

}

1 个答案:

答案 0 :(得分:1)

似乎您遇到的问题源于这样一个事实,即将单元格标记为已选中并在该单元格上处理长按手势事件是分开的。我对你的问题的解释是你有一个单一选择(不是多个)的表格视图,并且你希望单元格通过正常点击选择动作以及通过识别长按手势来“选择”。但是 - 当您希望使用长按手势选择单元格时,您希望longpress通过正常点击产生与选择不同的操作(例如,如果用户点击要启动视图控制器A的单元格但是如果用户长时间按下你要启动视图控制器B的那个单元格,并且在这两种情况下你都希望表格将单元格视为“已选中”...让我知道你想要的是不是这个,我可以更新答案。

对于表视图,这不是一种非常常见的行为,但我们可以通过稍微修改您的代码来使其工作:

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

@property (assign, nonatomic) BOOL longPressActive;

然后在handleLongPress方法中,告诉表选择行:

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

}

最后,在表视图委托方法中,定义选择后期望的行为。请注意,在示例中长按任何单元格将导致显示相同的视图控制器。为了以不同的方式设置该视图控制器,您可以按照与your prior question中的答案类似的过程进行操作,或者可以在实例化后将行特定数据传递给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
    }

} 

希望这有用。