Ily*_*ski 34 iphone cocoa-touch uitableview
如何在某个单元格中检测到双击UITableView?
即如果用户进行一次触摸,我想执行一个动作,如果用户进行了双重触摸,我想执行另一个动作?我还需要知道触摸的索引路径.
我怎样才能实现这个目标?
谢谢.
los*_*sit 31
如果您不想创建子类UITableView,请使用带有表视图的计时器didSelectRowAtIndex:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//checking for double taps here
if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
//double tap - Put your double tap code here
[tapTimer invalidate];
[self setTapTimer:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Double Tap" message:@"You double-tapped the row" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
else if(tapCount == 0){
//This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
tapCount = tapCount + 1;
tappedRow = indexPath.row;
[self setTapTimer:[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimerFired:) userInfo:nil repeats:NO]];
}
else if(tappedRow != indexPath.row){
//tap on new row
tapCount = 0;
if(tapTimer != nil){
[tapTimer invalidate];
[self setTapTimer:nil];
}
}
}
- (void)tapTimerFired:(NSTimer *)aTimer{
//timer fired, there was a single tap on indexPath.row = tappedRow
if(tapTimer != nil){
tapCount = 0;
tappedRow = -1;
}
}
Run Code Online (Sandbox Code Playgroud)
HTH
oxi*_*gen 27
在您的UITableView班级中覆盖此方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(((UITouch *)[touches anyObject]).tapCount == 2)
{
NSLog(@"DOUBLE TOUCH");
}
[super touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*lly 10
在您的UITableView子类中,执行以下操作:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch* touch in touches) {
if (touch.tapCount == 2)
{
CGPoint where = [touch locationInView:self];
NSIndexPath* ip = [self indexPathForRowAtPoint:where];
NSLog(@"double clicked index path: %@", ip);
// do something useful with index path 'ip'
}
}
[super touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
首先定义:
int tapCount;
NSIndexPath *tableSelection;
Run Code Online (Sandbox Code Playgroud)
作为.h文件中的类级变量,并进行所有必要的设置.然后...
- (void)tableView(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
tableSelection = indexPath;
tapCount++;
switch (tapCount) {
case 1: //single tap
[self performSelector:@selector(singleTap) withObject: nil afterDelay: .4];
break;
case 2: //double tap
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
[self performSelector:@selector(doubleTap) withObject: nil];
break;
default:
break;
}
}
#pragma mark -
#pragma mark Table Tap/multiTap
- (void)singleTap {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Single tap detected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
tapCount = 0;
}
- (void)doubleTap {
NSUInteger row = [tableSelection row];
companyName = [self.suppliers objectAtIndex:row];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"DoubleTap" delegate:nil cancelButtonTitle:@"Yes" otherButtonTitles: nil];
[alert show];
tapCount = 0;
}
Run Code Online (Sandbox Code Playgroud)
if([touch tapCount] == 1)
{
[self performSelector:@selector(singleTapRecevied) withObject:self afterDelay:0.3];
} else if ([touch tapCount] == 2)
{
[TapEnableImageView cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapRecevied) object:self];
}
Run Code Online (Sandbox Code Playgroud)
使用performSelector来调用选择器而不是使用计时器.这解决了@ V1ru8提到的问题.
我选择通过覆盖它来实现它UITableViewCell.
MyTableViewCell.h
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, assign) int numberOfClicks;
@end
Run Code Online (Sandbox Code Playgroud)
MyTableViewCell.m
@implementation MyTableViewCell
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
self.numberOfClicks = [aTouch tapCount];
[super touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)