UITableViewCell with multiple gestures: long click and click

As the question says, I would like to implement two different actions for the crane and a long click on the UITableViewCell.

I believe that I need to deselect the line at each stage and not enter any functions here:

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

And then add Tap Gestures from the storyboard, but the Storyboard will give me an error when I drag and drop actions into cell prototypes. Tips

+4
source share
2 answers

try it -

In your cellForRowAtIndexPath method cellForRowAtIndexPath you can add a gesture gesture and a hard print gesture separately, and then implement them. However, your didselect function didselect not required, and you do not need 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 } 
+14
source

You can handle a one-time action using the UITableViewDelegate method that you specified above,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

To execute the UILongPressGestureRecognizer on your UITableViewCell, follow these instructions .

Hope this helps.

0
source

Source: https://habr.com/ru/post/1498812/


All Articles