2012-01-05 73 views
3

我有一个tableView完成填充视图。当我点击一个单元格tableView:didSelectRowAtIndexPath:正在被解雇,但是当我写touchesBegan:withEvent来查找tableView的touch和pinch事件时,它并未被解雇。 Tableview在视图上方是不触发的原因,如果是的话如何找到tableView或其某个单元格被触摸或捏住时。如何找到一个tableView单元格被触摸或捏在iPhone?


我找到了一种方法找到挤压或触摸的表视图细胞,首先声明UIPinchGestureRecognizer 对象的tableView:didSelectRowAtIndexPath方法: 即

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; 
[tblAccountsList addGestureRecognizer:pinch]; 
[pinch release]; 

然后下面写的选择方法...

- (void)handlePinch:(UIGestureRecognizer *)recognizer { 
    NSLog(@"Pinch"); 
} 

当我设置断点它正在执行,但该方法被调用了3次。 如果任何一个找到解决股吧,请...

在此先感谢...

+0

对不起,我不知道hw做到这一点,帮助我... – 2012-01-10 12:18:01

回答

10

的touchesBegan是一个UIView和UITableViewCell的方法,而不是一个UIViewController &的UITableViewController方法。因此您可以为UITableViewCell创建自定义类,它可以识别触摸事件并触摸它为我工作的委托。

//TableViewCell.h 

#import <UIKit/UIKit.h> 

@class Util; 

@interface TableViewCell : UITableViewCell { 

} 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; 

@end 
    //TableViewCell.m 
#import "TableViewCell.h" 
@implementation TableViewCell 

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  format:(NSString*)ec_format{ 

    if (self) { 
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    } 

    return self; 
} 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
//you receive touch here 
    NSLog(@"Category Touch %@",self.frame); 


} 

在表查看您可以添加

- (void)viewDidLoad { 

[super viewDidLoad]; 

// Add a pinch gesture recognizer to the table view. 
UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; 
[self.tableView addGestureRecognizer:pinchRecognizer]; 

// Set up default values. 
self.tableView.sectionHeaderHeight = HEADER_HEIGHT; 
/* 
The section info array is thrown away in viewWillUnload, so it's OK to set the default values here. If you keep the section information etc. then set the default values in the designated initializer. 
*/ 
rowHeight_ = DEFAULT_ROW_HEIGHT; 
openSectionIndex_ = NSNotFound; 
} 


#pragma mark Handling pinches 


-(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer { 

/* 
There are different actions to take for the different states of the gesture recognizer. 
* In the Began state, use the pinch location to find the index path of the row with which the pinch is associated, and keep a reference to that in pinchedIndexPath. Then get the current height of that row, and store as the initial pinch height. Finally, update the scale for the pinched row. 
* In the Changed state, update the scale for the pinched row (identified by pinchedIndexPath). 
* In the Ended or Canceled state, set the pinchedIndexPath property to nil. 
*/ 

if (pinchRecognizer.state == UIGestureRecognizerStateBegan) { 

    CGPoint pinchLocation = [pinchRecognizer locationInView:self.tableView]; 
    NSIndexPath *newPinchedIndexPath = [self.tableView indexPathForRowAtPoint:pinchLocation]; 
    self.pinchedIndexPath = newPinchedIndexPath; 

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:newPinchedIndexPath.section]; 
    self.initialPinchHeight = [[sectionInfo objectInRowHeightsAtIndex:newPinchedIndexPath.row] floatValue]; 
    // Alternatively, set initialPinchHeight = uniformRowHeight. 

    [self updateForPinchScale:pinchRecognizer.scale atIndexPath:newPinchedIndexPath]; 
} 
else { 
    if (pinchRecognizer.state == UIGestureRecognizerStateChanged) { 
     [self updateForPinchScale:pinchRecognizer.scale atIndexPath:self.pinchedIndexPath]; 
    } 
    else if ((pinchRecognizer.state == UIGestureRecognizerStateCancelled) || (pinchRecognizer.state == UIGestureRecognizerStateEnded)) { 
     self.pinchedIndexPath = nil; 
    } 
} 
} 


-(void)updateForPinchScale:(CGFloat)scale atIndexPath:(NSIndexPath*)indexPath { 

if (indexPath && (indexPath.section != NSNotFound) && (indexPath.row != NSNotFound)) { 

    CGFloat newHeight = round(MAX(self.initialPinchHeight * scale, DEFAULT_ROW_HEIGHT)); 

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:indexPath.section]; 
    [sectionInfo replaceObjectInRowHeightsAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:newHeight]]; 
    // Alternatively, set uniformRowHeight = newHeight. 

    /* 
    Switch off animations during the row height resize, otherwise there is a lag before the user's action is seen. 
    */ 
    BOOL animationsEnabled = [UIView areAnimationsEnabled]; 
    [UIView setAnimationsEnabled:NO]; 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
    [UIView setAnimationsEnabled:animationsEnabled]; 
} 
} 
+0

我有捏的问题... – 2012-01-09 12:21:26

+0

嗨,朋友,我编辑我的代码,并添加了捏为tableview,因为scrollview只有捏的响应UITableView是滚动视图对我来说工作正常。如果您需要更多帮助[访问](http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139)并下载示例代码 – 2012-01-09 13:15:43

1

它被称为3倍,因为它的每一次所谓的手势识别器的状态发生变化,而不是每次手势被识别:)

试试这个变化:

- (void)handlePinch:(UIGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateRecognized) { 
     NSLog(@"Pinch"); 
    } 
} 

并检查了该文档为gesture recognisers here,特别是state属性。

相关问题