我正在寫一個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