2013-04-03 50 views
0

我使用下面的代碼顯示刪除刷卡的UITableViewCell像收藏夾按鈕

- (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 
    } 
} 

-(void)swipePressed:(UISwipeGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint p = [gestureRecognizer locationInView:self.myTable]; 
    NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:p]; 
    if (indexPath == nil) 
     NSLog(@"long press on table view but not on a row"); 
    else 
    { 
     [[self.myTable cellForRowAtIndexPath:indexPath] setEditingAccessoryType: UITableViewCellEditingStyleDelete]; 
    } 
} 

的swipePressed運行,但沒有刪除按鈕顯示出來......

+0

剛編輯我的答案。 – daltonclaybrook

回答

2

你不需要安裝滑動手勢識別以實現UITableView的輕掃 - 刪除功能。此功能由UITableView代理免費提供給您。這也不是setEditingAccessoryType的正確用法。完全刪除滑動手勢識別器和方法,然後執行該方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleDelete; 
} 
+0

沒有工作。你能解釋更多嗎? –

+0

thanKS男人,你解決了它 –

相關問題