業務需要給cell添加一個長按手勢
//需要在這個方法里添加 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //添加長按手勢 UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)]; longPressGesture.minimumPressDuration=1.5f;//設置長按 時間 [cell addGestureRecognizer:longPressGesture]; return cell; }
-(void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer{ if (longRecognizer.state==UIGestureRecognizerStateBegan) { //成為第一響應者,需重寫該方法 [self becomeFirstResponder]; CGPoint location = [longRecognizer locationInView:self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location]; //可以得到此時你點擊的哪一行 //在此添加你想要完成的功能 } }
#pragma mark 實現成為第一響應者方法 -(BOOL)canBecomeFirstResponder{ return YES; }