How to Enable Swipe to Delete Cell in a Tableview

How to enable swipe to delete cell in a TableView?

You don't have to set editing:YES if you need to show Delete button on cell swipe. You have to implement tableView:canEditRowAtIndexPath: and return YES from there for rows you need to edit/delete. This is not necessary when your tableView's dataSource is a subclass of UITableViewContoller - this method, if not overridden, returns YES by default. In all other cases you have to implement it.

EDIT: Together we have found the problem - tableView:editingStyleForRowAtIndexPath: returned UITableViewCellEditingStyleNone if table wasn't in editing mode.

Add swipe to delete UITableViewCell

Add these two functions:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}

Swift 3.0:

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}

Swift 4.2

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}

UITableView disable swipe to delete for particular cells swift

You might customize the UITableViewDelegate's function editingStyleForRowAt, especially returning UITableViewCellEditingStyle.none when you don't need the swipe, something like:

public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
if mainList[indexPath.row].statusId == "12" {
return UITableViewCellEditingStyle.none
} else {
return UITableViewCellEditingStyle.delete
}
}

How to enable swipe to delete cell in a TableView?

You don't have to set editing:YES if you need to show Delete button on cell swipe. You have to implement tableView:canEditRowAtIndexPath: and return YES from there for rows you need to edit/delete. This is not necessary when your tableView's dataSource is a subclass of UITableViewContoller - this method, if not overridden, returns YES by default. In all other cases you have to implement it.

EDIT: Together we have found the problem - tableView:editingStyleForRowAtIndexPath: returned UITableViewCellEditingStyleNone if table wasn't in editing mode.

How to swipe-to-delete in a TableView with custom Table View cells?

First of all don't use two dictionaries as data source use one array of a custom struct

struct Task {
var name, time : String
}


var tasks = [Task]()

override func viewDidLoad() {
super.viewDidLoad()

// Set initial taskTime value
tasks.append(Task(name:"", time: "Set time"))

taskList.delegate = self
taskList.dataSource = self
}

And don't hard-code the number of cells, count the array, taskCount is not needed.

extension TaskListViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return tasks.count
}

// Return custom cell + data to insert in table view
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskCell

cell.delegate = self

// Configure nameField and timeButton in taskCell
let task = tasks(indexPath.row)
cell.nameField.text = task.name
cell.timeButton.setTitle(task.time, for: .normal)

return cell
}

And it's highly recommended to use tableView(_:trailingSwipeActionsConfigurationForRowAt: rather than tableView(_:commit:forRowAt

In this method remove the item at the given index path and call deleteRows(at:with:). Calling reloadData() right after deleteRows(at:with:) is redundant. The latter methods updates the UI

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

let delete = UIContextualAction(style: .destructive, title: "Delete") { [unowned self] action, view, completionHandler in
self.tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
}

Of course you have to refactor the other occurrences of the two data source dictionaries to match the data source array.

iOS - How To Give A Peek At The Swipe To Delete Action On A Table View Cell?

I'm pretty sure this can't be done. The swipe actions are contained in a UISwipeActionPullView, which contains UISwipeStandardAction subviews, both of which are private. They are also part of the table view, not the cell, and they're not added unless a gesture is happening, so you can't just bump the cell to one side and see them there.

Outside of UI automation tests, it isn't possible to simulate user gestures without using private API, so you can't "fake" a swipe and then show the results to the user.

However, why bother doing it "properly" when you can cheat? It shouldn't be too hard to bounce the cell's content view slightly to the left, and bounce in a red box (not so far that you can see text, to avoid localisation issues), then return to normal. Do this on the first load of the table view, and stop doing it after N times or after the user has proven that they know how this particular iOS convention works.

UITableViewCell, show delete button on swipe

During startup in (-viewDidLoad or in storyboard) do:

self.tableView.allowsMultipleSelectionDuringEditing = false

Override to support conditional editing of the table view. This only needs to be implemented if you are going to be returning NO for some items. By default, all items are editable.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}

Swipe to delete from TableView not working

The UITableViewDelegate implements the method

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)

which can be used to configure a swipe action - a delete in your case. Here is a example I use in one of my projects:

extension ExampleViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive,
title: "Delete") { [weak self] _, _, complete in
tableView.deleteRows(at: [indexPath], with: .automatic)
complete(true)
}
deleteAction.backgroundColor = .red

return UISwipeActionsConfiguration(actions: [deleteAction])
}

// instead of a gesture recognizer you can use this delegate method
// it will be called whenever you tap on a cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// whatever you gesture recognizer did
}
}

Since your GestureRecognizer on the entire view gives you trouble, I'll recommend using a third party implementation or apples public API to handle dismissing your viewController instead of implementing it yourself. In that case you can use the UIModalPresentationStyle of your viewController.

func startExampleViewController() {
exampleViewController.modalPresentationStyle = .formSheet
previousViewController.present(exampleViewController, animated: true)
}


Related Topics



Leave a reply



Submit