我希望只需轻按一下UITableViewCell
。我为UITableview
创建了一个自定义DataSource。
我怎样才能做到这一点?
答案 0 :(得分:17)
执行此操作的正确方法是在tableView上添加UITapGestureRecognizer:
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:doubleTap];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.tableView addGestureRecognizer:singleTap];
然后在你的回调方法中,你会发现这样的单元格:
-(void)singleTap:(UITapGestureRecognizer*)tap
{
if (UIGestureRecognizerStateEnded == tap.state)
{
CGPoint p = [tap locationInView:tap.view];
NSIndexPath* indexPath = [_tableView indexPathForRowAtPoint:p];
UITableViewCell* cell = [_tableView cellForRowAtIndexPath:indexPath];
// Do your stuff
}
}
答案 1 :(得分:7)
我通过将以下内容添加到您的手势识别器中来实现此目的,但我发现单次点击时会有轻微的延迟。
[tapRecognizer setDelaysTouchesBegan:YES];
[tapRecognizer setCancelsTouchesInView:YES];
答案 2 :(得分:1)
你忘了[doubleTap release];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[cell addGestureRecognizer:doubleTap];
[doubleTap release];
答案 3 :(得分:0)
您需要使用此行以分隔单击和双击。 [singleTap requireGestureRecognizerToFail:doubleTap];这是示例代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// After cell initialized code
//...
//...
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[singleTap requireGestureRecognizerToFail:doDoubleTap];
[cell addGestureRecognizer:singleTap];
[cell addGestureRecognizer:doubleTap];
return cell;
}
-(void)doubleTap:(UITapGestureRecognizer*)tap
{
NSLog(@"Double Taps");
CGPoint point = [tap locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint: point];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
// do your stuff.
// ...
}
-(void)singleTap:(UITapGestureRecognizer*)tap
{
NSLog(@"Single Tap");
CGPoint point = [tap locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:point];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Do you stuff with index path or cell here.
// ...
}
答案 4 :(得分:-1)
好的,这次我在单元格上添加了Button,覆盖了完整的宽度和高度。 和我使用的UIBUTTON触摸事件。
[btnCellReview addTarget:self action:@selector(multipleTap:withEvent:)
forControlEvents:UIControlEventTouchDownRepeat];
-(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event {
UITouch* touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
// do action.
}
}