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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With