我正在写一个iOS 5应用程序(在Xcode 4.3中,使用Storyboard和ARC),它有一些表格单元需要响应水平锅。我有一个表格设置工作得很好,但我需要在另一个场景上实现相同的行为。我认为最佳实践方式是将手势识别和处理代码抽象为子类。但是现在tableView不会滚动,我在旧方法下解决这个问题的解决方案并没有帮助。为什么不是我的子类的UItableView滚动?
我有一个RestaurantViewController
继承自UIViewController
并有一个属性ULPanningTableView *tableView
。表中的一些单元格为MenuItemCell
,并从ULPanningTableViewCell
继承。该表的代表和数据源是RestaurantViewController
。
ULPanningTableViewCell
从UITableViewCell
继承而来,非常接近原始,唯一的区别是它具有跟踪单元格的正面和背面视图的属性以及自定义背景。
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
东西在RestaurantViewController
和ULPanningTableViewCell
内部实现的东西是在MenuItemCell
实现的。我基本上会返回NO的手势,其中t他translationInView
比水平更垂直)。无论如何,我无法让桌子滚动!如果我从gestureRecognizerShouldBegin:
返回YES,或者我完全删除了UIGestureRecognizerDelegate
实现,我可以获得平移手势。
我还是iOS的初学者,并且在Objective-C中,所以我只根据我读过的东西准备好臀部,而我从similar problem的印象中得知,罪魁祸首是UIScrollViewPanGestureRecognizer
做巫术响应者链...
我将不胜感激任何光线,你可以摆脱这个!
你们是不是要在单元格中移动视图或者是你想水平滚动单元格的内容? – NJones 2012-04-01 17:45:59
水平锅应该移动单元格中的视图。我无法弄清楚如何与单元格的内容进行交互以显示其“backgroundView”,因此我正在移动单元格“UIView * frontView”的自定义属性。垂直平移应该滚动表格。 – 2012-04-02 08:08:21