时间:2021-05-01 09:32:03 | 栏目:iOS代码 | 点击:次
前言
本文主要给大家介绍了关于iOS11解决UITableView侧滑删除无限拉伸的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ ... }
iOS11如果仍然用以上方法处理侧滑删除功能,但是会发现侧滑无限拉伸,然后自动调用删除方法
iOS11新增了方法
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){ if (@available(iOS 11.0, *)) { NSString *title = @"置顶"; if (indexPath.section == 0) { title = @"取消置顶"; } else { title = @"置顶"; } UIContextualAction *topAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:title handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { ... // 这句很重要,退出编辑模式,隐藏左滑菜单 [tableView setEditing:NO animated:YES]; completionHandler(true); }]; UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { // 这句很重要,退出编辑模式,隐藏左滑菜单 [tableView setEditing:NO animated:YES]; completionHandler(true); }]; UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,topAction]]; // 禁止侧滑无线拉伸 actions.performsFirstActionWithFullSwipe = NO; return actions; }else{ return nil; } }
iOS11之前,并不会发生侧滑无线拉伸
总结