长按TableView的Cell做一些操作时,就要先捕获TableView的长按事件,然后判断是第几行,或者直接对Cell监控长按事件,对于性能来说,当然是前者好一点,也就是说捕获TableView的长按事件,如下:
.在viewDidLoad事件中加入:
OC代码
2 | UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] |
3 | initWithTarget:self action:@selector(handleLongPress:)]; |
4 | lpgr.minimumPressDuration = 1.0; |
5 | [self.tableView addGestureRecognizer:lpgr]; |
监控函数handleLongPress的内容如下:
OC代码
01 | -( void )handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer |
03 | CGPoint p = [gestureRecognizer locationInView:self.tableView]; |
04 | NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; |
05 | if (gestureRecognizer.state == UIGestureRecognizerStateBegan) |
07 | UIAlertView * addView = [[UIAlertView alloc] initWithTitle: @"提示"
message: @"加入书签,是否继续?"
delegate :self cancelButtonTitle: @"确定"
otherButtonTitles: @"取消" ,nil]; |
08 | addView.alertViewStyle = UIAlertViewStyleDefault; |
10 | self.tableviewcurrentcoursename=[[self.fetchedResultsController objectAtIndexPath:indexPath] name]; |
12 | else
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) |
14 | NSLog( @"UIGestureRecognizerStateEnded" ); |
16 | else
if (gestureRecognizer.state == UIGestureRecognizerStateChanged) |
18 | NSLog( @"UIGestureRecognizerStateChanged" ); |
20 | else
if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) |
22 | NSLog( @"UIGestureRecognizerStateCancelled" ); |
24 | else
if (gestureRecognizer.state ==UIGestureRecognizerStateFailed) |
26 | NSLog( @"UIGestureRecognizerStateFailed" ); |