2013-12-10 133 views
7

我的UITableView有一個單元集合。滑動單元格會導致出現「刪除」按鈕。點擊此刪除按鈕將導致執行以下代碼:第一次點擊UITableView後忽略deleteRowsAtIndexPaths

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
[self.itemList removeObjectAtIndex:indexPath.row]; 

這正確地導致從列表中刪除單元格。但是,當我點擊UITableVIew中剩餘的單元格中的一個單元格來選擇它時,該分支將被忽略(即不調用tableView:didSelectRowAtIndexPath)。如果我再次點擊,則它可以正常工作(即調用tableView:didSelectRowAtIndexPath)。

刪除後我點擊哪個單元並不重要,無論我在刪除之後等待多長時間,刪除之後的第一次敲擊總是被忽略,刪除之後的第二次敲擊成功。

基於各種StackOverflow的答案,我曾嘗試:

  • 刪除
  • 刪除
  • 刪除
  • 調用[self.tableView beginUpdates];之前刪除後調用[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];後調用[self.tableView reloadData];後設置self.tableView.editing = NO;[self.tableView endUpdates];刪除後
  • 在同一時間完成以上所有操作e

以上都沒有幫助;刪除後的第一次點擊仍然始終被忽略。

更新: 我還在上面的代碼片段中添加了[self.itemList removeObjectAtIndex:indexPath.row];代碼。 我的數據源委託方法是這樣的:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.itemList.count; 
} 

中的代碼片段被調用的doDelete方法來響應一個按鈕龍頭([self.deleteButton addTarget:self action:@selector(doDelete) forControlEvents:UIControlEventTouchUpInside];

更新#2:基於Lyssa的意見,我曾嘗試以下:我刪除我們的自定義按鈕,刪除所有引用和我們的滑動手勢,然後將此代碼添加到我們的UITableView委託:

-(BOOL) tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

-(void) tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.itemList removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
} 

但這工作 - 我現在可以刪除行和下一個水龍頭確實工作正常但是,這消除了我們對錶格單元格的自定義外觀,這是我們自定義代碼的目的。也許我應該看看使用上述方法並自定義刪除按鈕(我做了一些尋找,但沒有找到一個好的答案呢)。

+0

您是否從numberOfRowsInSection減少計數:刪除單元格後。 – Greg

+0

你是否更新了你的tableview的數據源,刪除你刪除的單元格的數據對象? – thorb65

+0

你在哪裏放了deleteRowsAtIndexPath? – asjj

回答

0

感謝所有誰提供的答案或意見,我的問題解決了。您的意見幫助我追蹤了這個問題。

事實證明,我們正在實施tableView:shouldHighlightRowAtIndexPath,並且我們在刪除之後首次返回NO,然後從此返回YES。這是第一個水龍頭被忽略的原因。我們完全有問題的代碼是這樣的:

-(BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.currentPath) { 
     [self resetSelectedCell]; 
     self.currentPath = nil; 
     return NO; 
    } 
    return YES; 
} 

是提供此代碼,以防止在一次顯示不止一個刪除按鈕,但我們並沒有經過正確刪除重置self.currentPath。我們現在將self.currentPath設置爲零,刪除成功並且代碼正常工作。

4

問題是,當您使用輕掃即可刪除僅滑動的行進入編輯模式。

而且第一次輕觸即將結束編輯模式該行:

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { 
} 

你可以通過設置編輯模式

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.itemList removeObjectAtIndex:indexPath.row]; 
     [self.tableView setEditing:NO animated:YES]; 
    } 
    [self.tableView reloadData]; 
} 
+0

由於[文檔](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:commitEditingStyle:forRowAtIndexPath: ):「你不應該在這個方法的實現中調用'setEditing:animated:'。如果由於某種原因你必須在延遲後通過使用'performSelector:withObject:afterDelay:'方法來調用它。 – Shebuka