【问题标题】:Swift - Swipe to delete (without the delete button) in a static UITableViewSwift - 在静态 UITableView 中滑动以删除(没有删除按钮)
【发布时间】:2017-06-23 23:54:05
【问题描述】:

我想实现一个功能,使用 Swift 3.0 将整个表格单元格滑开。就像在 iOS 10 的邮件应用中滑动删除一样。

需要注意的是,我并不是指滑动显示删除按钮,然后处理删除。我的意思是用户可以在屏幕上从右向左滑动来删除。 (还要注意tableView是静态的,不是动态的)

我是否必须为此实现 UISwipleGestureRecognizer?

【问题讨论】:

  • 是的,您可以在 tableview 的特定单元格上使用 swipegesture

标签: ios swift xcode uitableview swift3


【解决方案1】:

您只需添加这些功能并将其更改为您想要的需求。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
         }

还有这个

    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        let example1 = UITableViewRowAction(style: .normal, title: "example1") { (action: UITableViewRowAction!, indexPath: IndexPath!) -> Void in
            //  run your desired action
        }

        example1.backgroundColor = .white

        let example2 = UITableViewRowAction(style: .normal, title: "example2") { (action: UITableViewRowAction!, indexPath: IndexPath!) -> Void in
            //  run your desired action
        }

        example2.backgroundColor = .green

        let example3 = UITableViewRowAction(style: .normal, title: "example3") { (action: UITableViewRowAction!, indexPath: IndexPath!) -> Void in
            //  run your desired action
        }

        example3.backgroundColor = .orange

        return [example1, example2, example3]

    }

【讨论】:

  • 这不是问题的答案
  • 他说不要按钮,滑动删除就行了。
【解决方案2】:

您可以实现UITableViewDataSource 方法tableView(_:commit:forRowAt:)tableView(_:canEditRowAt:),就像对具有动态内容的表格一样。

但是,在包含静态内容的表中,您不能插入或删除行。由于表是静态的,因此行应该保持不变。您可以做的是更新您的滑动行以反映已删除的状态。

例如:在我的应用程序中,我使用内置的滑动手势来重置设置。用户滑动行(静态内容),点击删除按钮,应用程序会相应地设置该单元格中的标签和图像。该行不会被删除,它只是更新,就像您通常对静态单元格所做的那样(分配出口并像访问其他静态内容一样访问它们)。

编辑: 另请查看 iOS 11 中 UISwipeActionsConfiguration 类的 performsFirstActionWithFullSwipe 属性。将其设置为 true 以允许完全滑动手势,无需安装您自己的识别器。

您确实提到了 iOS 10 邮件;我不知道 Apple 在 iOS 10 中是否或如何做到这一点,因为此属性是 iOS 11 的一项功能。 iOS Mail 也很可能有一个包含动态内容的表格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多