简体   繁体   English

Swift - 在静态UITableView中滑动删除(不带删除按钮)

[英]Swift - Swipe to delete (without the delete button) in a static UITableView

I want to implement a feature, to swipe the whole table cell away using Swift 3.0. 我想实现一个功能,使用Swift 3.0刷掉整个表格单元格。 Like how you can swipe to delete in iOS 10's Mail app. 就像你可以在iOS 10的Mail应用程序中滑动删除一样。

Important to note that, I don't mean swipe to show a delete button, and then handle the deletion. 重要的是要注意,我不是指滑动显示删除按钮,然后处理删除。 I mean users can swipe from right to left across the screen to delete. 我的意思是用户可以在屏幕上从右向左滑动以删除。 (Also note that the tableView is static, not dynamic) (另请注意,tableView是静态的,不是动态的)

Do I have to implement a UISwipleGestureRecognizer for that? 我必须为此实现UISwipleGestureRecognizer吗?

You can implement the UITableViewDataSource methods tableView(_:commit:forRowAt:) and tableView(_:canEditRowAt:) as you would for a table with dynamic content. 您可以像对具有动态内容的表一样实现UITableViewDataSource方法tableView(_:commit:forRowAt:)tableView(_:canEditRowAt:)

However, in a table with static content you cannot insert or delete rows. 但是,在包含静态内容的表中,您无法插入或删除行。 With the table being static, the rows are supposed to stay. 由于表是静态的,所以行应该保留。 What you can do is update your swiped row to reflect the deleted status. 您可以做的是更新刷过的行以反映已删除的状态。

For example: In my app I am using the built-in swipe gesture to reset a setting. 例如:在我的应用程序中,我使用内置滑动手势来重置设置。 The user swipes the row (static content), taps the Delete button, and the app sets the labels and images in that cell accordingly. 用户滑动行(静态内容),点击“删除”按钮,应用程序相应地设置该单元格中的标签和图像。 The row is not deleted, it is just updated, as you normally do with static cells (assign outlets and access them like other static content). 该行不会被删除,它只是更新,就像您通常使用静态单元格一样(分配插座并像其他静态内容一样访问它们)。

Edit: Also check out the performsFirstActionWithFullSwipe property of the UISwipeActionsConfiguration class in iOS 11. Set it to true to allow the full swipe gesture, no need to install a recognizer of your own. 编辑:还检查了performsFirstActionWithFullSwipe的财产UISwipeActionsConfiguration类的iOS 11.将其设置为true ,让全划动手势,无需安装自己的识别。

You did mention iOS 10 Mail; 你确实提到过iOS 10 Mail; I don't know if or how Apple does it in iOS 10 since this property is an iOS 11 feature. 我不知道Apple是否或如何在iOS 10中使用它,因为此属性是iOS 11功能。 Also iOS Mail very likely has a table with dynamic content. iOS Mail也很可能有一个包含动态内容的表。

You simply have to add these functions and change them to your desired needs. 您只需添加这些功能并根据需要进行更改。

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

and this 和这个

    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]

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM