2013-05-03 74 views
0

我試圖在我的一個表上實現UILongPressGesture以便能夠重新排列其單元格。但是,每次我長時間按下我的應用程序崩潰。嘗試實施UILongPressGesture時應用程序崩潰

下面是實現UILongPressGesture文件:

#import "LstrTableViewDragToMove.h" 
#import "LstrTableViewCell.h" 

@implementation LstrTableViewDragToMove 
{ 
    LstrTableView *_tableView; 
    BOOL _dragInProgress; 
    CGPoint _initialTouchPoint; 
    int _cellIndex; 
    LstrTableViewCell *_currentCell; 

} 

-(id)initWithTableView:(LstrTableView *)tableView 
{ 
    self = [super init]; 
    if(self) 
    { 
     _tableView = tableView; 
     UIGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; 
     [_tableView addGestureRecognizer:recognizer]; 
    } 

    return self; 

} 

-(BOOL) viewContainsPoint:(UIView*)view withPoint:(CGPoint)point { 
    CGRect frame = view.frame; 
    return (frame.origin.y < point.y) && (frame.origin.y + frame.size.height) > point.y; 
} 

-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer 
{ 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     [self longPressStarted:recognizer]; 
    } 

    if (recognizer.state == UIGestureRecognizerStateEnded) { 
     [self longPressEnded:recognizer]; 
    } 
} 

-(void)longPressStarted:(UILongPressGestureRecognizer *)recognizer 
{ 
    NSLog(@"Started"); 
    /* 
    _initialTouchPoint = [recognizer locationOfTouch:0 inView:_tableView.scrollView]; 
    NSArray* visibleCells = _tableView.visibleCells; 
    for (int i=0; i < visibleCells.count; i++) { 
     UIView* cell = (UIView*)visibleCells[i]; 
     if ([self viewContainsPoint:cell withPoint:_initialTouchPoint]) { 
      _cellIndex = i; 
     } 
    } 

    _currentCell = _tableView.visibleCells[_cellIndex]; 
    _currentCell.label.allowsEditingTextAttributes = NO; 
    _currentCell.frame = CGRectMake(_currentCell.frame.origin.x, _currentCell.frame.origin.y, _currentCell.frame.size.width + 10.0f, _currentCell.frame.size.height + 10.0f); 
    [_currentCell addDropShadow]; 
    */ 
} 


-(void)longPressEnded:(UILongPressGestureRecognizer *)recognizer 
{ 
    NSLog(@"Ended"); 
} 

@end 

而這裏的錯誤日誌:

2013-05-03 13:17:53.750 Lstr[19207:907] -[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x20856240 
2013-05-03 13:17:53.754 Lstr[19207:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x20856240' 
*** First throw call stack: 
(0x3302a2a3 0x3ad5297f 0x3302de07 0x3302c531 0x3302d938 0x6db43 0x34f4f1ab 0x34f1863f 0x34f482a5 0x33941f53 0x32fff5df 0x32fff291 0x32ffdf01 0x32f70ebd 0x32f70d49 0x36b492eb 0x34e86301 0x66775 0x3b189b20) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

在此先感謝。

UPDATE:

下面是具有translationInView方法:

#pragma mark - horizontal pan gesture methods 
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { 
    CGPoint translation = [gestureRecognizer translationInView:[self superview]]; 
    // Check for horizontal gesture 
    if (fabsf(translation.x) > fabsf(translation.y)) { 
     return YES; 
    } 
    return NO; 
} 

回答

-1

嘗試,聲明你的tableview作爲

@property(nonatomic,retain)LstrTableView *_tableView; 
2

是否使用translationInView地方在頭文件的屬性在你的代碼?

您應該將其更改爲locationInView

+0

就是這樣。我不是。 – Sam 2013-05-03 10:51:21

+0

您能否爲所有異常添加斷點並查看代碼崩潰的位置? – 2013-05-03 10:54:55

+0

哦,等等,我在另一個文件中使用translationInView,這就是我的問題所在。我要更新我的問題以顯示具有translationInView的方法。 – Sam 2013-05-03 11:03:59

相關問題