2013-08-25 58 views
4

正如問題所述,我想對UITableViewCell上的輕敲和長按兩個不同的操作。帶有多個手勢的UITableViewCell:長按並點擊

我想我不得不取消在每個階段的行,並把沒有函數中的位置:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

再從故事板添加輕敲姿勢,但故事板給我一個錯誤,當我拖曳動作爲原型細胞。提示?

回答

0

您可以處理的UITableViewDelegate你上面貼的方法單輕叩動作,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

爲您的UITableViewCell follow this instructions做UILongPressGestureRecognizer。

希望有所幫助。

14

試試這個 -

在你cellForRowAtIndexPath方法,你可以單獨添加輕敲手勢和長按手勢,然後實現它們。通過這個你的didselect函數將不會被要求,你不會必須deSelect任何東西。

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)]; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    tapGestureRecognizer.numberOfTouchesRequired = 1; 
    cell.tag = indexPath.row; 
    [cell addGestureRecognizer:tapGestureRecognizer]; 


UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
         initWithTarget:self action:@selector(handleLongPress:)]; 
     lpgr.minimumPressDuration = 1.0; //seconds 
     [cell addGestureRecognizer:lpgr]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

現在,

-(void)handleLongPress:(UILongPressGestureRecognizer *)longPress 
{ 
    // Your code here 
} 

-(void)cellTapped:(UITapGestureRecognizer*)tap 
{ 
    // Your code here 
} 
+0

你好,這是爲我工作,,但它要執行此handleLongpress方法兩次,,我創建手勢並將其添加到細胞withing cellAtIndex方法 – user2889249

+0

它應該如上所述在cellForRowAtIndexPath中完成。它不應該調用同樣的方法兩次 –

+2

對於'UILongPressGestureRecognizer',你應該總是檢查狀態屬性,否則你可能會得到很多的調用。例如:'recognizer.state == UIGestureRecognizerStateBegan' –