2012-01-30 27 views

回答

6

我還沒有嘗試過,並且實施了這個,但我會給它一個鏡頭。首先,創建一個自定義的UITableViewCell,讓它有2個屬性,你可以使用

  1. 一個參考的tableView它正在被使用。
  2. 的indexPath找到TableView中的單元格。(注意,這需要每次刪除所有單元格時更新)

在您的cellForRowAtIndexPath:,您創建自定義單元格,設置這些屬性。同時在單元格中添加一個UISwipeGestureRecognizer

cell.tableView=tableView; 
cell.indexPath=indexPath; 
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
[cell addGestureRecognizer:swipeGestureRecognizer]; 
[swipeGestureRecognizer release]; 

確保手勢僅接收水平滑動。

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
     ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
     ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
} 

在你deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec 
{ 
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
    CustomCell *cell=[swipeGestureRecognizer view]; 
    UITableView *tableView=cell.tableView; 
    NSIndexPath *indexPath=cell.indexPath; 
    //you can now use these two to perform delete operation 
} 
+0

不要忘記,首先初始化手勢識別器時,需要設置手勢識別器的方向以將其添加到單元格中。像這樣:swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris 2013-12-05 20:26:08

+0

蘋果,該死的你不提供'(void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector'。 – 2014-03-10 17:24:59

-2

因爲你必須在你的項目中添加這段代碼。

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
    }  
} 

否則鐵道部信息ü可以通過這個鏈接 enter link description here

+0

Vivek2012和iWill - 感謝您的回覆,但我不想顯示DELETE按鈕,已經有在stackoverflow中的人的答案。 我希望細胞在接收到左手或右手滑動手勢的瞬間消失。請閱讀我的描述,謝謝 – carbonr 2012-01-30 06:54:52

-1

實施

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 

這兩種方法,如果UITableViewCellEditingStyle是UITableViewCellEditingStyleDelete,然後做某事與

抹去你的
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

就是這樣。

3

發表@MadhavanRP解決方案的工作,但它比它需要更加複雜。您可以採取更簡單的方法,並創建一個手勢識別器來處理表中發生的所有滑動,然後獲取滑動的位置以確定用戶滑動的單元格。

要設置手勢識別:

- (void)setUpLeftSwipe { 
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                 action:@selector(swipeLeft:)]; 
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.tableView addGestureRecognizer:recognizer]; 
    recognizer.delegate = self; 
} 

調用此方法viewDidLoad

要處理的刷卡:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    ... do something with cell now that i have the indexpath, maybe save the world? ... 
} 

注:你的VC必須實現手勢識別委託。

相關問題