Add Swipe to Delete Uitableviewcell

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)
}
}

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
}
}

Is there any way to swipe to delete/ Add actions into cell in UITableview Diffable datasource?

I solved this issue by following method:
Used subclass of diffabled datasource as follows.

class GrocDataSource: UITableViewDiffableDataSource <GrocListSections, Grocs> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 || indexPath.section == 2 {
return true
} else {
return false
}
}

}

Implemented this class into datasource as:

datasource = GrocDataSource(tableView: grocTableView, cellProvider: { (tableView, indexPath, grocs) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: "GrocNameCell", for: indexPath) as? GrocNameCell else {return UITableViewCell()}
return cell })

Swipe to delete should delete the row and add a new one in UITableView

You need to do something like this.

tableView.beginUpdates()
// Remove existing row
cardTransactions.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .none)

// Add new row
listDataSource.insert(object, at: indexPath.row)
tableView.insertRows(at: [indexPath], with: .none)
tableView.endUpdates()

Table View: right to left swipe to delete does not show up - swift 3

Did you implement tableView:canEditRowAtIndexPath: method?

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

EDIT:

Thanks to @rmaddy for mentioning that the default value of tableView:canEditRowAtIndexPath: is true, implementing it doesn't solve the problem.

I'm not pretty sure of what are you trying to do from your code snippet, so make sure that you are implementing the following methods (UITableViewDelegate):

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
toDoList.remove(at: indexPath.row)

UserDefaults.standard.set(toDoList, forKey: "toDoList")
tableView.reloadData()
}
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}

You can also keep the implementation of tableView:canEditRowAtIndexPath: method:

Asks the data source to verify that the given row is editable.

So, -for example- if you want to let the first row is not editable, i.e user cannot swipe and delete the first row, you should do somthing like:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 0 {
return false
}

return true
}

Make sure that the UITableViewDataSource and UITableViewDelegate are connected with the ViewController.

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.

Full swipe to delete UITableViewCell and partial swipe to show options

This repo has what you want. Have a look.

https://github.com/MortimerGoro/MGSwipeTableCell



Related Topics



Leave a reply



Submit