How can I detect a double tap on a certain cell in UITableView?

后端 未结 14 1053
悲哀的现实
悲哀的现实 2020-12-01 00:18

How can I detect a double tap on a certain cell in UITableView?

i.e. I want to perform one action if the user made a single touch and a

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 00:50

    I chose to implement it by overriding the UITableViewCell.

    MyTableViewCell.h

    @interface MyTableViewCell : UITableViewCell
    
    @property (nonatomic, assign) int numberOfClicks;
    
    @end
    

    MyTableViewCell.m

    @implementation MyTableViewCell
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       UITouch *aTouch = [touches anyObject];
       self.numberOfClicks = [aTouch tapCount];
       [super touchesEnded:touches withEvent:event];
    }
    

    TableViewController.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
       MyTableViewCell *myCell = (MyTableViewCell*) [self.tableView cellForRowAtIndexPath:indexPath];
    
       NSLog(@"clicks:%d", myCell.numberOfClicks);
    
       if (myCell.numberOfClicks == 2) {
           NSLog(@"Double clicked");
       }
    }
    

提交回复
热议问题