0

我正在写一个iOS 5应用程序(在Xcode 4.3中,使用Storyboard和ARC),它有一些表格单元需要响应水平锅。我有一个表格设置工作得很好,但我需要在另一个场景上实现相同的行为。我认为最佳实践方式是将手势识别和处理代码抽象为子类。但是现在tableView不会滚动,我在旧方法下解决这个问题的解决方案并没有帮助。为什么不是我的子类的UItableView滚动?

我有一个RestaurantViewController继承自UIViewController并有一个属性ULPanningTableView *tableView。表中的一些单元格为MenuItemCell,并从ULPanningTableViewCell继承。该表的代表和数据源是RestaurantViewController

ULPanningTableViewCellUITableViewCell继承而来,非常接近原始,唯一的区别是它具有跟踪单元格的正面和背面视图的属性以及自定义背景。

ULPanningTableView有点复杂,因为它必须设置识别和处理。

ULPanningTableView.h

#import <UIKit/UIKit.h> 

@interface ULPanningTableView : UITableView <UIGestureRecognizerDelegate> 

@property (nonatomic) float openCellLastTX; 
@property (nonatomic, strong) NSIndexPath *openCellIndexPath; 

- (id)dequeueReusablePanningCellWithIdentifier:(NSString *)identifier; 
- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer; 
// ... some helpers for handlePan: 

@end 

ULPanningTableView.m

#import "ULPanningTableView.h" 
#import "ULPanningTableViewCell.h" 

@implementation ULPanningTableView 

@synthesize openCellIndexPath=_openCellIndexPath, openCellLastTX=_openCellLastTX; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

#pragma mark - Table View Helpers 

- (id)dequeueReusablePanningCellWithIdentifier:(NSString *)identifier 
{ 
    ULPanningTableViewCell *cell = (ULPanningTableViewCell *)[self dequeueReusableCellWithIdentifier:identifier]; 

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [panGestureRecognizer setDelegate:self]; 
    [cell addGestureRecognizer:panGestureRecognizer]; 

    return cell; 
} 

#pragma mark - UIGestureRecognizerDelegate protocol 

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    // for testing: only allow UIScrollViewPanGestureRecognizers to begin 
    NSString *gr = NSStringFromClass([gestureRecognizer class]); 
    if ([gr isEqualToString:@"UIScrollViewPanGestureRecognizer"]) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

#pragma mark - panhandling 

- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer 
{ 
    // ... 
} 
// ... some helpers for handlePan: 

@end 

我已经gestureRecognizerShouldBegin:玩耍了,因为这是我如何解决这个问题,回来时,这些都不是单独的类(ULPanningTableView东西在RestaurantViewControllerULPanningTableViewCell内部实现的东西是在MenuItemCell实现的。我基本上会返回NO的手势,其中t他translationInView比水平更垂直)。无论如何,我无法让桌子滚动!如果我从gestureRecognizerShouldBegin:返回YES,或者我完全删除了UIGestureRecognizerDelegate实现,我可以获得平移手势。

我还是iOS的初学者,并且在Objective-C中,所以我只根据我读过的东西准备好臀部,而我从similar problem的印象中得知,罪魁祸首是UIScrollViewPanGestureRecognizer做巫术响应者链...

我将不胜感激任何光线,你可以摆脱这个!

+0

你们是不是要在单元格中移动视图或者是你想水平滚动单元格的内容? – NJones 2012-04-01 17:45:59

+0

水平锅应该移动单元格中的视图。我无法弄清楚如何与单元格的内容进行交互以显示其“backgroundView”,因此我正在移动单元格“UIView * frontView”的自定义属性。垂直平移应该滚动表格。 – 2012-04-02 08:08:21

回答

0

好的,所以我觉得很傻。 -handlePan:已经是一种方法!我将它更改为-handleCustomPan:,它通常会处理其他平底锅。我不确定它为什么没有崩溃,但它在那里。哦,我必须保持在-gestureRecognizerDidBegin:的UIScrollViewPanGestureRecognizer边缘情况:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    NSString *gr = NSStringFromClass([gestureRecognizer class]); 
    if ([gr isEqualToString:@"UIScrollViewPanGestureRecognizer"]) { 
     // allow scroll to begin 
     return YES; 
    } else if ([gr isEqualToString:@"UIPanGestureRecognizer"]){ 
     UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *)gestureRecognizer; 
     // allow horizontal pans to begin 
     ULPanningTableViewCell *cell = (ULPanningTableViewCell *)[panGR view]; 
     CGPoint translation = [panGR translationInView:[cell superview]]; 
     BOOL should = (fabs(translation.x)/fabs(translation.y) > 1) ? YES : NO; 

     if (!should) { 
      [self closeOpenCellAnimated:YES]; 
     } 

     return should; 

    } else { 
     NSLog(@"%@",gestureRecognizer); 
     return NO; 
    } 
}