更改长按uiTableView IOS上的单元格选择 [英] Change Selection of Cell on Long Press uiTableView IOS

查看:52
本文介绍了更改长按uiTableView IOS上的单元格选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Normal selection of cell on uitableview is working fine. But when long press event is called for a cell it selects the previously selected cell again. For example if user selected 1st cell then long press the second cell ,, event for long press is called for the second cell but selection goes back again to first cell.

这是我的代码:

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

}

推荐答案

您遇到的问题似乎源于以下事实:将单元格标记为选中状态和处理该单元格上的长按手势事件是分开的.我对您的问题的解释是,您有一个单选(不是多个)的表格视图,并且您希望单元格通过普通点击选择操作以及通过识别长按手势来选择".但是 - 虽然您希望通过长按手势选择单元格,但您希望长按导致与通过普通点击选择不同的操作(例如,如果用户点击您想要启动视图控制器 A 但如果用户长按您想要启动视图控制器 B 的单元格,并且在这两种情况下您都希望表格将单元格视为已选择"...如果您想要的内容与此不同,请告诉我,我可以更新答案.

It seems that the issue you are having is rooted in the fact that marking a cell as selected and handling a long press gesture event on that cell are separate. My interpretation of your question is that you have a table view with single selection (not multiple) and that you want the cells to become 'selected' both via the normal tap to select action and also by recognizing a long press gesture. However -- while you want the cell to become selected with the long press gesture, you would like the longpress to result in a different action than selection via a normal tap (e.g. if the user taps a cell you want to launch view controller A but if the user long presses that cell you want to launch view controller B, and in both cases you want the table to regard the cell as 'selected) ...let me know if what you want is different than this, and I can update the answer.

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

This isn't a very common behavior for table views, but we can make it work by modifying your code a bit:

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

First define a property to keep track of whether a long press is happening:

@property (assign, nonatomic) BOOL longPressActive;

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

Then in your handleLongPress method, tell the table to select the row:

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

}

最后,在您的表视图委托方法中,定义您在选择后期望的行为.请注意,在示例中,长按任何单元格将导致显示相同的视图控制器.为了以不同的方式设置该视图控制器,您可以按照类似于我在 您之前的问题 或者您可以在实例化之后将行特定数据传递给editViewController.

Finally, in your table view delegate methods, define the behavior you expect after selection. Note that in the is example a long press on any cell will result in the same view controller displaying. In order to set that view controller up differently you can follow a process similar to my answer in your prior question or you can pass row specific data to the editViewController after you instantiate it.

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

} 

希望这会有所帮助.

这篇关于更改长按uiTableView IOS上的单元格选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆