首页 > 移动平台 > 详细

iOS UITableView 侧滑

时间:2020-03-13 11:37:39      阅读:100      评论:0      收藏:0      [点我收藏+]

默认的侧滑

实现以下方法,则可以得到默认的侧滑删除。并且在该方法处理点击事件。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

得到以下效果:

 

 
技术分享图片
default delete.png

修改标题

如果想要改变按钮的问题,重写以下方法设置即可:

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"返回";
}
 
技术分享图片
backTitle.png

侧滑显示多个按钮

在iOS 8 之后,可以实现以下方法,可以得到多个按钮,每个按钮都是固定的宽度,设置太多会超出屏幕。而且,在block中处理不同按钮的点击事件。

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    // 收回侧滑
    [tableView setEditing:NO animated:YES];
}];


UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    // 删除cell: 必须要先删除数据源,才能删除cell
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];

return @[deleteAction, editAction, deleteAction, editAction];
}
 
技术分享图片
multiTitle.png
editAction.backgroundColor = [UIColor yellowColor];

其中,如果设置了UITableViewRowAction的背景颜色,则显示用户设置的。否则显示系统默认的,Destructive是红色,Normal 是浅灰色。

按钮显示图标

有一个取巧的方法,可以用平铺的图片设置成UITableViewRowAction的backgroundColor, 但是这个图标需要大一点,不然就会被平铺了。

UIImage *image = [UIImage imageNamed:@"account_item_me"];
editAction.backgroundColor = [UIColor colorWithPatternImage:image];
 
技术分享图片
bigImage.png

 

 
技术分享图片
smallImage.png

iOS 11 之后,新增了两个方法, 可以直接设置图标。trailingSwipe是左滑,显示右侧菜单。leadingSwipe是右
滑,显示左侧菜单。

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
if (@available (iOS 11, *)) {
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        if (self.dataArray.count > indexPath.row) {
            [self.dataArray removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }

        completionHandler(YES);
    }];

    deleteAction.image = [UIImage imageNamed:@"public_system_success"];

    UIContextualAction *backAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        // 不做任何事
        [tableView setEditing:NO animated:YES];
        completionHandler(YES);
    }];
    backAction.image = [UIImage imageNamed:@"public_system_success"];

    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, backAction]];

    return configuration;
}

return nil;
}

但是不知道哪里出错了,没有正确地显示图标,而是出现了白色圆圈。其实,应该是显示跟Cell左侧一样的图标才对。如果图片太大的话,还会将其他的菜单给覆盖了。

 

 
技术分享图片
strangeWhiteImage.png

右滑,显示左侧的菜单,跟左滑的一样。

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction *editAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
    // 不做任何事,只是收回侧滑
    [tableView setEditing:NO animated:YES];
    completionHandler(YES);
}];

editAction.backgroundColor = [UIColor greenColor];

UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[editAction]];

return configuration;
}
 

iOS UITableView 侧滑

原文:https://www.cnblogs.com/chenweb/p/12484446.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!