Swiping right on a UITableViewCell - ios

So i've searched the site for an answer to this question and there are some decent results but nothing recent since Xcode 7 is no longer in beta and swift 2.0 is now the standard.
I've used the following code in order to make it so that a 'swipe left' feature will cause something to happen to a UITableViewCell -
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete {
// ...
}
}
I understand that this is something that Apple now supplied in their API and use of external classes is not needed.
I also understand you can customize the actions that come up from this swipe using this native code:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
print("more button tapped")
}
Is there any modern native code which would define a 'right swipe' on a UITableViewCell?

func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Closed")
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [closeAction])
}
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Update", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
modifyAction.image = UIImage(named: "hammer")
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}

The feature is available on iOS 11.0+ and Mac Catalyst 13.0+. For options on the left use the leading and on the right use the trailing methods.
Just implement the UITableViewDelegate methods and return your UISwipeActionsConfiguration object. You could optionally include an image or just the text as the title of the button.
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
Make sure to enable editing in your data source delegate otherwise the swipe options are disabled.
// True for enabling the editing mode
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

No, there Is no such native API as of now.

Related

SwipeActions does not work in tableView swift 5 xcode 13.4.1

i am doing trailingSwipe to my tableview but it does not work. does not call the function trailingSwipeActionsConfigurationForRowAt.
try with the leadingSwipeActionsConfigurationForRowAt and if it works for me but not with trailingSwipe
valid that I have delegate and datasource the tableview
code
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .normal, title: "delete2") {(action, view, completionHandler) in
print("delete2 \(indexPath.row)")
}
let swipe = UISwipeActionsConfiguration(actions: [delete])
return swipe
}
Uses Apple Mac M1 ( MacBook Pro M1)
Try this, working fine on the Xcode 13+, Apple Mac M1 and Swift 5
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
// print("index path of delete: \(indexPath)")
completionHandler(true)
}
let edit = UIContextualAction(style: .normal, title: "Edit") { [weak self] (action, sourceView, completionHandler) in
completionHandler(true)
}
delete.title = ""
delete.image = UIImage(named: "deleteProfile")
edit.title = ""
edit.image = UIImage(named: "editProfile")
let swipeAction = UISwipeActionsConfiguration(actions: [delete,edit])
swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
return swipeAction
}
I also faced this issue on my end.
Maybe this issue is with the Apple Mac M1 please try with the Apple intel.
Please check this question
Below code is working fine in my Apple Intel.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .destructive, title: "REMOVE") { [self] action, view, completion in
// Your swipe action code!
completion(true)
}
action.backgroundColor = UIColor.red
let swipeAction = UISwipeActionsConfiguration(actions: [action])
swipeAction.performsFirstActionWithFullSwipe = false // Full swipe disable
return swipeAction
}
I found a solution in Apple M1 please try it with the Real device.
I tried the above code on a real device it's working perfectly.

UITableViewRowAction was deprecated in iOS 13.0 [duplicate]

This question already has answers here:
UITableViewRowAction vs UISwipeActionsConfiguration
(4 answers)
Closed 3 years ago.
I am trying to upgrade code of my project and found this warning
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteRowAction = UITableViewRowAction(style: .destructive, title: deleteActionTitle) { [unowned self] (_, indexPath) in
//code you want to execute }
return [deleteRowAction]
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let contextItem = UIContextualAction(style: .destructive, title: deleteActionTitle) { (contextualAction, view, boolValue) in
//Code I want to do here
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}
You can use the UISwipeActionsConfiguration over UITableViewRowAction such as
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return UISwipeActionsConfiguration()
}

UITableVIew swipe trailingSwipeActionsConfigurationForRowAt not working

New iOS 11 UITableView Swipe action not getting called. The delegate and datasource are working fine for the table.
I am not able to swipe and see the menu items.
Below is my code for the same.
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
print("Leading Action style .normal")
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let contextItem = UIContextualAction(style: .destructive, title: "Trailing & .destructive") { (contextualAction, view, boolValue) in
print("Trailing Action style .destructive")
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}
I tried to call below and its working fine.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
}
}
Any hint in right direction is highly appreciated.
You need to pass true to UIContextualAction in the closure boolValue(true). Otherwise the handler won't allow the action.
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
boolValue(true) // pass true if you want the handler to allow the action
print("Leading Action style .normal")
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}
For those who uses UITableViewDiffableDataSource you need to subclass it and override its canEditRowAt method.
public override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
true
}
Code looks good, but you are missing probably missing the following the "UITableViewDelegate" in the Class declaration and also the "TableView.delegate = self" in the viewdidload() section. So it should look like:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}

How to show image when triggering UITableViewRowAction?

I'm trying to customize the where the delete option has a red background and a trash icon. So far from what I gathered and implemented I have a repeating image in the background and not sure how to make it red or show only 1 trash can image
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let title = " "
let deleteAction = UITableViewRowAction(style: .default, title: title) { (action, indexpath) in
}
let image = UIImage(named: "icon_trash")
if let im = image {
deleteAction.backgroundColor = UIColor(patternImage: im)
}
return [deleteAction]
}
I can see I'm close but not sure how to piece the color and what I have together to show something like the image below
I think it's time to upgrade to these ios 11 new methods with image property
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
So try this
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
// Call edit action
// Reset state
success(true)
})
deleteAction.image = UIImage(named: "icon_trash")
deleteAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [deleteAction])
}

Change the text of the cell delete button

i'm trying to change the delete button of a cell.
I have 2 functions :
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let titleBtn = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Supprimer") { (action , indexPath ) -> Void in
self.isEditing = false
//ackAction.backgroundColor = UIColor.orange
}
return [titleBtn]
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
But, when I run my app, the text of the button is changed but the delete doesn't work (i can't delete data from my array and the row of my tableview). Before to add this functions all worked perfectly.
A detail: in the canEditRowAt function, I tried to return false too...
Thanks by advance
if you want to change the text of the delete button, conform this method in the UITableViewDelegate:
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "Your new title"
}
To delete the item from the array conform this method
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if (editingStyle == UITableViewCellEditingStyle.delete)
{
yourDataSourceArray.remove(at: indexPath.row)
yourTableView.reloadData()
}
}
You can either use default delete action and change its title like this.
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "MyAction"
}
or you can create your own action buttons like this.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete") {action in
//handle delete
}
let editAction = UITableViewRowAction(style: .normal, title: "Edit") {action in
//handle edit
}
return [deleteAction, editAction]
}

Resources