snapfish
snapfish

Reputation: 175

using tap gesture to select row

I want to click on each cell to activate a push segue but because the view controller is always in editing mode I cannot click on the cells. In editing mode, I have enlarged the reorder controller to cover the entire cell and then made it invisible so you can drag to reorder tableview from anywhere in the cell thus when I click on the cells in editing mode, I'm actually clicking on the reorder controller. Please do not tell me to set the tableview property of allowsSelectionDuringEditing to YES as I have already done that and it still does not let me select the cell because it is being covered by the reorder controller which was added to the cell as a subview.

My solution was to prepare a new push segue via a tap gesture in the cell which would still activate even if the cell was covered by its reorder controller. However, the data that is sent over to the view that is called by the segue is always from the first cell no matter which cell I click in. Why does this happen? Any help is appreciated.

this is how I changed my reorder control

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

Upvotes: 3

Views: 1273

Answers (1)

Ossir
Ossir

Reputation: 3145

Here's solution that works for me.

To find reorder control in iOS7, I've changed the code and added one more check

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

Add to your view controller declaration UIGestureRecognizerDelegate delegate, your view controller interface should look like this:

@interface ViewController <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>

Then implement delegate method:

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

Also you need to implement method that we specified in UITapGestureRecognizer init method:

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

Upvotes: 1

Related Questions