Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell with multiple gestures: long-press and tap

As the question states, I would like to implement two different actions for a tap and a long-press on a UITableViewCell.

I reckon I have to deselect the row at each stage and put no functions inside here:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

And then add Tap Gestures from storyboard, but Storyboard give me an error when I drag actions to prototype cells. Hints?

like image 202
piggyback Avatar asked Aug 25 '13 09:08

piggyback


1 Answers

try this -

In your cellForRowAtIndexPath method, you can add tap gesture and long press gesture separately and then implement them. By this your didselect function will not be required and you wont have to deSelect anything.

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.numberOfTouchesRequired = 1;
    cell.tag = indexPath.row;
    [cell addGestureRecognizer:tapGestureRecognizer];


UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                         initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 1.0; //seconds
        [cell addGestureRecognizer:lpgr];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Now,

-(void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
    // Your code here
}

-(void)cellTapped:(UITapGestureRecognizer*)tap
{
    // Your code here
}
like image 70
anky_believeMe Avatar answered Oct 18 '22 15:10

anky_believeMe