2012-03-18 55 views
3

的拖動事件可能重複:
How to get notified of UITableViewCell move start and end檢測的UITableViewCell

我已經實現了定製設計UITableView。這UITableView必須支持編輯模式。當進入編輯模式時,UITableViewCell被附加控件(EditControl,ReorderControl ...)裝飾。他們不適合我的定製設計,這就是爲什麼我想要替換他們的圖像。爲此,我分類UITableViewCell並覆蓋layoutSubview,我替換這些控件的圖像。

問題:當開始拖動操作時,EditControl的圖像被替換回原來的UITableViewCell中的某個位置。我可以再次替換它

– tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: 

當用戶將可拖動的單元格移動到另一個indexPath時,但這太遲了。

所以我的問題歸結爲:如何檢測用戶實際開始拖動UITableViewCell的那一刻?

+0

使用PanGesture檢測圖像拖動操作的時刻 – Bala 2012-03-19 07:57:08

+0

感謝您的評論。已經嘗試過,沒有成功。我可以將GestureRecognizer附加到單元格的contentview,並且我的處理程序被解僱。但它不會因ReorderControl的觸發而被解僱。重寫touchesBegan也是如此... – 2012-03-19 15:51:30

+0

發佈您的代碼可能有助於找到解決方案:) – Bala 2012-03-19 15:56:48

回答

0

雖然Bala的評論走向了正確的方向,但我最初在正確實施時遇到了一些問題。現在我發現它是如何做到的。簡而言之:您必須創建一個UITableViewCell的自定義子類。重寫layoutSubviews以將UILongPressGestureRecognizer附加到UITableViewCellReorderControl。定義一個協議,並使用一個委託通知誰想要關於拖動狀態。

CustomTableViewCell.h:

#import <UIKit/UIKit.h> 

@protocol CustomTableViewCellDelegate; 

@interface CustomTableViewCell : UITableViewCell { 
} 

@property (nonatomic, assign) id <CustomTableViewCellDelegate> delegate; 

@end 

@protocol CustomTableViewCellDelegate 
- (void)CustomTableViewCell:(CustomTableViewCell *)cell isDragging:(BOOL)value; 
@end 

CustomTableViewCell.m:

#import "CustomTableViewCell.h" 

@implementation CustomTableViewCell 

@synthesize delegate = _delegate; 

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { 
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     [_delegate CustomTableViewCell:self isDragging:YES]; // Dragging started 
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     [_delegate CustomTableViewCell:self isDragging:NO];  // Dragging ended 
    } 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    for (UIView *view in self.subviews) { 
     if ([NSStringFromClass ([view class]) rangeOfString:@"ReorderControl"].location != NSNotFound) { // UITableViewCellReorderControl 
      if (view.gestureRecognizers.count == 0) { 
       UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
       gesture.cancelsTouchesInView = NO; 
       gesture.minimumPressDuration = 0.150; 
       [view addGestureRecognizer:gesture]; 
      } 
     } 
    } 
} 

@end 

要知道,儘管這個代碼不使用任何私有的API,如果蘋果改變其內部實現它仍可能會停止工作(即通過更改UITableViewCellReorderControl的類名)。

相關問題