Change the text of the cell delete button - ios

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

Related

trailingSwipeActionsConfigurationForRowAt not work in stability

I am trying to make swipe-able table view cell for iOS 11 and above and trying to use the trailingSwipeActionsConfigurationForRowAt but it works on iPad and doesn't work on iPhone. It is not called in iPhone.
Surprisingly if I try to swipe the cell more then 10 or 20 times it sometimes works for once.
Here is my controller's table view extension
import UIKit
extension CustomViewController: UITableViewDelegate, UITableViewDataSource {
func initTableView() {
tableView.register(cell: SingleLineListItemViewCell.self)
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 64
tableView.rowHeight = UITableView.automaticDimension
tableView.backgroundColor = .green
tableView.isEditing = false
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = SingleLineListItemViewModel(title: "coko")
let cell = tableView.dequeueReusableCell(withIdentifier: SingleLineListItemViewCell.reuseIdentifier) as? SingleLineListItemViewCell
cell?.model = model
cell?.isFirst = indexPath.row == 0
cell?.isLast = indexPath.row == 2
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 64.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("didSelectRowAt....")
router?.routeToTransfer()
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
#available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return nil
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// Action here
// In case of delete, you can simply do:
if editingStyle == .delete {
//Remove item at relative position from datasource array
//Reload tableview at the respective indexpath
}
}
#available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, _ in
//YOUR_CODE_HERE
}
deleteAction.backgroundColor = .red
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
}
Any help would be much appreciated.
The only obvious thing wrong with this is that you're not calling the completion handler. According to the API documentation it is necessary to call the completion handler to indicate whether the operation was successful. What it actually does I have no idea...
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in
//YOUR_CODE_HERE
completionHandler(true)
}
deleteAction.backgroundColor = .red
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
If this doesn't make any difference, do you have any other gesture recognisers that could be conflicting with the swipe action?
you can try this one
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "Delete"
}
// this method handles row deletion
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// remove the item from the data model
// delete the table view row
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Not used in our example, but if you were adding a new row, this is where you would do it.
}
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(
style: .normal,
title: "Delete",
handler: { (action, view, completion) in
//do what you want here
completion(true)
})
action.backgroundColor = .red
let configuration = UISwipeActionsConfiguration(actions: [action])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
Here is are actions defined as well (point 1 and 2);
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "") { _, _, completionHandler in
// 1. remove object from your array
scannedItems.remove(at: indexPath.row)
// 2. reload the table, otherwise you get an index out of bounds crash
self.tableView.reloadData()
completionHandler(true)
}
deleteAction.backgroundColor = .systemOrange
deleteAction.image = UIImage(named: "trash")
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = true
return configuration
}

UITableView not showing Delete button on clicking minus button(Only on iOS 13)

I have a table view to shows a list of items. I have implemented the default editing style delete. It's been working till iOS 12. But not on iOS 13. It does not show the Delete button when clicking the minus button.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return machineOrders.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MachineOrderCell") as! MachineOrderCell
let machineOrder = machineOrders[indexPath.row]
cell.update(with: machineOrder)
cell.decreaseQuantityButton.isEnabled = canDecreaseQuantity(for: machineOrder)
cell.onDecreaseQuantity = {
self.delegate.view(self, didSelectDecreaseQuantity: machineOrder)
}
cell.onIncreaseQuantity = {
self.delegate.view(self, didSelectIncreaseQuantity: machineOrder)
}
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
navigationItem.rightBarButtonItem = doneBarButton
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
navigationItem.rightBarButtonItem = !isCartEmpty ? (tableView.isEditing ? doneBarButton : editBarButton) : nil
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let machineOrder = machineOrders[indexPath.row]
if editingStyle == .delete {
self.delegate.view(self, didSelectDelete: machineOrder)
}
}
Press minus button for some seconds, I mean long press, then it will work.

UITableViewCell row action while table is editing

I have a UITableView which I need to both reorder and allow for cells to be deleted by swiping left on a cell to reveal the Delete action. Is it possible to have both at the same time?
For the table I have:
tableView.allowsSelectionDuringEditing = true
tableView.isEditing = true
For the datasource:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// I do nothing here
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// This is where I rearrange my data source array
}
And for the delegate:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { action, index in
// delete row
}
delete.backgroundColor = UIColor.appRed
return [delete]
}
So what I want to have is:
Reorder control allowing me to move a cell
Swipe left to reveal cell actions
And I need these to be visible and work at the same time. Can this be done?
Swipe to delete is not possible when your UITableView is in editing mode.
If you want to hide the delete button on the left when in editing mode you have to implement the following methods:
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return tableView.isEditing ? .none : .delete
}
I think this is the closest you can get.

tableView's commit method stops being called if editActionsForRowAt is present

My tableView's commit method gets called fine when the user swipes and deletes a row, but if I attempt to customize the color/text of the action by using editActionsForRowAt then it stops being called.
Here's some code
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
return .delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
// delete code goes here
}
With the above code then tableView:commit will get called. But if I attempt to change the background color and text by adding the method below then it stops getting called.
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { (action, indexPath) in
}
delete.backgroundColor = UIColor.lightGray
return [delete]
}
Why does adding the editActionsForRowAt method stop the commit method from getting called?
When using editActionsForRowAt the commit function won't be called. Instead of putting your code in the commit function, put it in the closure on the UITableViewRowAction:
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { (action, indexPath) in
// delete code should go here instead
}
delete.backgroundColor = UIColor.lightGray
return [delete]
}

Swiping right on a UITableViewCell

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.

Resources