UITATIONVIEW SWIPE DELETE iOS 11不会刷新 /重新加载数据



我是编程的新手。以下代码可以使用Swipe删除一行,但是刷新后,所有行列表都会出现。我有以下代码:

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

在您记录删除的索引路径的完整汉格勒中,您需要从数据源(数组等)中删除该行。否则,当您的表从数据源重新加载时,它将重新出现。

您的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;
    }

这将在从表中视觉删除行时从数据源中删除对象。

最新更新