2017-09-22 111 views
0

我是编程新手。下面的代码使用滑动删除一行,但在刷新所有行列表后出现。 我有以下代码:UItableview滑动删除iOS 11不刷新/重新加载数据

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive 
                     title:@"DELETE" 
                     handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { 
                      NSLog(@"index path of delete: %@", indexPath); 
                      completionHandler(YES); 


                     }]; 
    delete.backgroundColor = [UIColor purpleColor]; //arbitrary color 

    UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]]; 
    swipeActionConfig.performsFirstActionWithFullSwipe = NO; 

    return swipeActionConfig; 

} 

我使用如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FCell = (favouriteCell *)[tableView dequeueReusableCellWithIdentifier:@"favouriteCell"]; 
    FCell.selectionStyle=UITableViewCellSelectionStyleNone; 
    if (FCell == nil) 
    { 
     FCell = [[[NSBundle mainBundle]loadNibNamed:@"favouriteCell" owner:nil options:nil] objectAtIndex:0]; 
    } 

    FCell.nameLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"name"]; 
    FCell.poetLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"poet"]; 
    return FCell; 

} 
+0

它似乎没有从任何数据源中删除该行。 在您正在记录删除的索引路径的completionHandler中,您需要从数据源(数组等)中删除该行。否则,当你的表从数据源重新加载时,它会重新出现。 – Stephen

+0

谢谢,最好的办法是做什么? – Simi

+0

你用什么结构作为数据源?你能分享你的cellForRowAtIndexPath的一些代码吗? – Stephen

回答

0

在completionHandler你在哪里记录删除的索引路径,则需要从数据源中删除该行(数组等)。否则,当你的表从数据源重新加载时,它会重新出现。

trailingSwipeActionsConfigurationForRowAtIndexPath代码应该是这样的:

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { 
     UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive 
                      title:@"DELETE" 
                      handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { 
                       NSLog(@"index path of delete: %@", indexPath); 
[favDetails removeObjectAtIndex:indexPath.row];             
completionHandler(YES); 


                      }]; 
     delete.backgroundColor = [UIColor purpleColor]; //arbitrary color 

     UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]]; 
     swipeActionConfig.performsFirstActionWithFullSwipe = NO; 

     return swipeActionConfig; 

    } 

作为行从表中删除视觉这将会从您的数据源中删除的对象。

+0

非常感谢,但它显示一个错误。我在我的问题中添加了NSlog代码。 – Simi

+0

我不确定您的问题中的新代码如何与原始代码相关。我相信我的回答可以解决你原来的问题。 你可能应该用你的错误开一个新的问题。 – Stephen

+0

为了清晰起见,我编辑了我的答案。 – Stephen