如何向右滑动以隐藏UITableView中的删除按钮?

时间:2018-01-31 19:17:42

标签: ios swift uitableview

当你在桌面视图单元格上向左滑动时,我当前让我的UITableView显示“删除”按钮。我想在同一个单元格上向右滑动隐藏此按钮。它应该是粘性的,以便在我向右滑动时隐藏它 - 就像它向左滑动时显示“删除”按钮的方式一样。现在,当我向左滑动以显示删除按钮时,我只能在滚动表格视图时隐藏它。是否有内置的方法来实现这种滑动权利隐藏行为?或者这需要是一个定制的解决方案吗?

编辑:我认为defualt Messages应用程序有这种行为,但我意识到当我向右滑动时,我正在滚动tableview,导致它隐藏。现在隐藏的唯一方法是点击单元格或滚动tableview

我已经设置了我的tableView,通过实现以下方法来显示删除按钮:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
   if editingStyle == .delete {
        print("deleted!")
   }     
}

1 个答案:

答案 0 :(得分:1)

是的,我在尝试允许用户滑动删除选项时也遇到了问题。 (类似于Apple Mail应用程序的工作方式)

我发现它能按预期工作的唯一方法是,如果您还包含了LeadingSwipeAction ...(如果用户最初向右或向左滑动,则会获得不同的选项)。添加另一面后,用户可以将“删除”滑走。

顺便说一句,“轻扫删除删除”听起来像是一首浓情emo歌曲。

这是我当前拥有的几乎可以完美运行的代码。偶尔会有图形异常,前导按钮向右跳转,但是我认为这可能是由于我认为某些其他代码造成的。该代码是从https://chariotsolutions.com/blog/post/uitableview-swipe-actions-in-ios-11/

大量借用的
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    if indexPath.section == Slider.Section.results.rawValue {
        return true
    } else {
        return false
    }

}

override func tableView(_ tableView: UITableView,
                        leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{

    let favoriteAction = self.contextualToggleFavoriteAction(forRowAtIndexPath: indexPath)

    return UISwipeActionsConfiguration(actions: [favoriteAction])

}

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {


    let delete = UIContextualAction(style: .destructive,
                                    title: "Delete") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

                                        Filter.allMarkers.remove(at: indexPath.row)
                                        tableView.deleteRows(at: [indexPath], with: .fade)
                                        completionHandler(true)

    }

    let share = UIContextualAction(style: .normal,
                                    title: "Share") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

                                        print("About to share -> \(Filter.allMarkers[indexPath.row].title ?? "")")
                                        completionHandler(true)

    }

    share.backgroundColor = Color.ocean

    let swipeConfig = UISwipeActionsConfiguration(actions: [delete, share])
    return swipeConfig
}


func contextualToggleFavoriteAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {

    let marker = Filter.allMarkers[indexPath.row]
    let action = UIContextualAction(style: .normal,
                                    title: "Favorite") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
                                        if marker.toggleFavoriteFlag() {
                                            completionHandler(true)
                                        } else {
                                            completionHandler(false)
                                        }
    }

    //action.image = UIImage(named: "flag")
    action.backgroundColor = marker.isFavorite ? Color.magenta : UIColor.gray
    return action
}