使用轻击手势选择行

时间:2014-02-26 07:19:25

标签: ios objective-c uitableview

我想点击每个单元格来激活push segue,但由于view controller始终处于编辑模式,我无法点击单元格。在editing mode中,我放大reorder controller以覆盖整个cell,然后将其隐藏,以便您可以拖动以从单元格中的任何位置重新排序tableview,这样当我点击在编辑模式下的单元格上,我实际上是在点击重新排序控制器。请不要告诉我将allowsSelectionDuringEditing的tableview属性设置为YES,因为我已经这样做了,它仍然不允许我选择单元格,因为它被重新排序控制器所覆盖,该控制器被添加到单元格中一个子视图。

我的解决方案是通过push segue中的tap手势准备一个新cell,即使该单元格被reorder controller覆盖,该手势仍会激活。但是,无论我点击哪个单元格,发送到view所调用的segue的数据始终来自第一个cell。为什么会发生这种情况?任何帮助表示赞赏。

这就是我改变我的重新排序控制的方式

-(void) resizeReorderControl: (UITableView *)tableView reorderCell:(UITableViewCell *)bCell{
    //  Grip customization code goes in here...
    UIView* reorderControl = [bCell huntedSubviewWithClassName:@"UITableViewCellReorderControl"];

    UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(reorderControl.frame), CGRectGetMaxY(reorderControl.frame))];
    [resizedGripView addSubview:reorderControl];
    [bCell addSubview:resizedGripView];

    CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - reorderControl.frame.size.width, resizedGripView.frame.size.height - reorderControl.frame.size.height);
    CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / reorderControl.frame.size.width, resizedGripView.frame.size.height / reorderControl.frame.size.height);

    //  Original transform
    CGAffineTransform transform = CGAffineTransformIdentity;

    //  Scale custom view so grip will fill entire cell
    transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);

    //  Move custom view so the grip's top left aligns with the cell's top left
    transform = CGAffineTransformTranslate(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);

    [resizedGripView setTransform:transform];

    for(UIImageView* cellGrip in reorderControl.subviews)
    {
        if([cellGrip isKindOfClass:[UIImageView class]])
            [cellGrip setImage:nil];
    }
}

1 个答案:

答案 0 :(得分:1)

这是适用于我的解决方案。

要在iOS7中找到重新排序控件,我已经更改了代码并添加了一个检查

-(void) resizeReorderControl: (UITableView *)tableView reorderCell:(UITableViewCell *)bCell{
    ...
    UIView* reorderControl = [bCell huntedSubviewWithClassName:@"UITableViewCellReorderControl"];
        if (!reorderControl) {
            reorderControl = [bCell huntedSubviewWithClassName:@"UITableViewCellScrollView"]; // for iOS7
        }
    ...
    //Then I added my gesture recognizer:
    ...
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapGestureRecognizer.delegate = self;
    [reorderControl addGestureRecognizer:tapGestureRecognizer];
    ...
}

添加到视图控制器声明UIGestureRecognizerDelegate委托,您的视图控制器界面应如下所示:

@interface ViewController <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>

然后实现委托方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

您还需要实现我们在UITapGestureRecognizer init方法中指定的方法:

- (void)handleTap:(UITapGestureRecognizer *)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[sender locationInView:self.tableView]];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    // If you have custom logic in table view delegate method, also invoke this method too
    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}