2013-12-10 129 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