I want to swipe the cell to the left to display the Delete button and the Details button.

Asked 2 years ago, Updated 2 years ago, 74 views

I'd like to swipe the cell to the left and display the Delete button and the Details button to delete the cell and transition to the Details page.

The Delete button successfully wrote the following code:

 functableView(_tableView:UITableView, commit editingStyle:UITableViewCellEditingStyle, forRowAtindexPath:IndexPath){
  ifeditingStyle==.delete{
      dataList.remove(at:indexPath.row)
     testTableView.deleteRows(at:[indexPath], with:.fade)
 } }

However, I don't know how to attach the detail button and the code of the screen transition when I press the detail button.
The ideal completion diagram is the following image.
Enter a description of the image here

Thank you for your cooperation.

swift tableview

2022-09-30 19:56

1 Answers

Use the tableView(_:editActionsForRowAt:) method of UITableViewDelegate. See the reference for the UITableViewRowAction class.
I will list the sample code.Table configuration is a prerequisite on the Storyboard.

import UIKit

classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        return12
    }

    // UITableViewDataSource
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        letcell=tableView.dueReusableCell(withIdentifier: "Cell", for:indexPath)
        cell.textLabel?.text="No.\(indexPath.row)"

        return cell
    }

    // UITableViewDelegate
    functableView(_tableView:UITableView, editActionsForRowAtindexPath:IndexPath)->[UITableViewRowAction]?{
        let action = UITableViewRowAction(style:.default, title: "Detail") {action, indexPath in
            // Do anything
        }

        return [action]
    }

}

//Do Anything describes what happens when you tap the button.

Enter a description of the image here


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.