2013-07-21 58 views
1

我想添加滑動手勢識別器到tableViewCell但它不起作用。添加手勢到TableViewCell

這是我創造我的手機的方式:

CellIdentifier = @"EventsSentCell"; 
    nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"EventsSentCell" owner:self options:nil]; 

EventsSentCell *cell = [[EventsSentCell alloc] init]; 
cell = (EventsSentCell *)[nibObjcet objectAtIndex:0]; 

,這是我的手機是如何在.m文件啓動:

-(id)init{ 
    self = [super init]; 
    if (self) { 
     leftSwipe = [[UISwipeGestureRecognizer alloc] init]; 
     leftSwipe.direction= UISwipeGestureRecognizerDirectionLeft; 
     [leftSwipe addTarget:self action:@selector(swipedLeft)]; 
     [self addGestureRecognizer:leftSwipe]; 
    } 
    return self; 
} 

,這是我宣佈我的手勢識別在.h文件中:

@property (nonatomic,strong) IBOutlet UISwipeGestureRecognizer *leftSwipe; 

但由於某種原因我的方法沒有被調用。

任何想法?

感謝

我試圖把下面的代碼:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ 
    NSLog(@"%@",gestureRecognizer); 
    return YES; 
} 

,結果我左刷卡後讓我的是:

<UILongPressGestureRecognizer: 0xa9d99a0; state = Possible; view = <UITableViewCellContentView 0xa9d8ce0>; target= <(action=_longPressGestureRecognized:, target=<EventsSentCell 0xa9d8bb0>)>> 

回答

0

回答實際問題之前,讓我指出你的代碼中的其他一些問題。

EventsSentCell *cell = [[EventsSentCell alloc] init]; 
cell = (EventsSentCell *)[nibObjcet objectAtIndex:0]; 

首先,這兩條線沒有意義。您正在分配並初始化一個沒有筆尖的EventSentCell實例。做完這些之後,您將覆蓋cell以指向由loadNibNamed:初始化的實例。您可以將其簡化爲EventsSentCell = (EventsSentCell *)nibObject[0];
但即使在進行了這些優化之後,仍然不推薦實施cellForRowAtIndexPath:。您應該在viewDidLoad中使用registerNib:forCellReuseIdentifier:,然後使用 dequeueReusableCellWithIdentifier:forIndexPath:獲得一個單元格,並且完全自行加載該筆尖。

接下來,

@property (nonatomic,strong) IBOutlet UISwipeGestureRecognizer *leftSwipe; 

您聲明此屬性作爲IBOutlet但你設置它(據我所知)只在代碼中,更具體的init方法。你可以完全忽略IBOutlet

而這個init方法可能也是你的問題的原因。當使用loadNibNamed實例化視圖時,將調用initWithCoder:而不是init。實現你的自定義初始化代碼(在這種情況下添加一個手勢識別器),它應該工作得很好。

0

您的'init'方法沒有被調用,所以手勢識別器沒有設置。

您可以嘗試初始化awakeFromNib而不是,但無論如何,您的單元格創建看起來非常規。

假設您正在使用帶有Xib文件的自定義單元類,以下是我該如何做的。

  1. 創建EventsSentCell對象的.m和.h文件

  2. 創建一個XIB文件 「EventsSentCell.xib」

  3. 在XIB文件,刪除默認的頂級視圖和替換它與一個UITableViewCell(你可以從對象庫中拖出一個)。在身份檢查器來更改它的類EventsSentCell

  4. 在你的viewController表的viewDidLoad ...

    UINib* EventsSentNib = [UINib nibWithNibName:@"EventsSentCell" 
                 bundle:nil]; 
    [self.tableView registerNib:EventsSentNib 
         forCellReuseIdentifier:@"EventsSentCell"]; 
    
  5. 在你cellForRowAtIndexPath方法:

    UITableViewCell *cell = 
         [tableView dequeueReusableCellWithIdentifier:@"EventsSentCell"]; 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"EventsSentCell"]; 
        } 
    
  6. 在EventsSentCell.m,觸發初始化代碼從-awakeFromNib

您的初始化代碼:

leftSwipe = [[UISwipeGestureRecognizer alloc] init]; 
    leftSwipe.direction= UISwipeGestureRecognizerDirectionLeft; 
    [leftSwipe addTarget:self action:@selector(swipedLeft)]; 
    [self addGestureRecognizer:leftSwipe]; 

會工作,因爲它是。

對於您的gestureRecogniser委託方法,您會得到UILongPressGestureRecognizer:...響應,因爲這是Apple提供的內置手勢識別器,它已將其代理設置爲單元格。當你的代碼是否正常工作,如果你還設置手勢recongnizer的委託到小區(leftSwipe.delegate = self),你希望看到一個simlilar日誌UISwipeGestureRecognizer:...

它也指出,UILongPressGestureRecognizer的觀點是不是細胞,但該單元的contentView。這是您的單元格視圖層次結構的超級視圖,您應該將其添加到您的單元格的所有內容中。雖然當您將它連接到電池的手勢識別的工作,我會建議在這裏下面的Apple:

 [self.contentView addGestureRecognizer:leftSwipe]; 

那麼姿勢會正確地按照小區的視圖層次結構。