How to show image when triggering UITableViewRowAction? - ios

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

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.

How to enable Leading and Trailing Swipe with Drag and Drop UITableview Cell Swift

I am trying to enable Leading and Trailing swipe with Long press tableview cell to drag and drop option using Swift. Here, I am using below code I can able to drag and drop it but can’t able to do long press also can't able to enable leading and trailing swipe at a time. Three things need to enable by default when app launched.
Tableview Delegate
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Delete")
success(true)
})
deleteAction.backgroundColor = .orange
return UISwipeActionsConfiguration(actions: [deleteAction])
}
override func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let modifyAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
self.showaddMilestone()
success(true)
})
modifyAction.image = UIImage(named: "edit")
modifyAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [modifyAction])
}
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedObject = self.milestoneTitles[sourceIndexPath.row]
milestoneTitles.remove(at: sourceIndexPath.row)
milestoneTitles.insert(movedObject, at: destinationIndexPath.row)
debugPrint("\(sourceIndexPath.row) => \(destinationIndexPath.row)")
// To check for correctness enable: self.tableView.reloadData()
}
You can do this in following way...
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
#available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
//EDIT
let actionEDIT = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionEDIT.image = UIImage(named: "icn_edit")
actionEDIT.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")
//PDF
let actionPDF = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionPDF.image = UIImage(named: "icn_pdf")
actionPDF.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")
//SHARE
let actionSHARE = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionSHARE.image = UIImage(named: "icn_shareGreen")
actionSHARE.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")
let configuration = UISwipeActionsConfiguration(actions: [actionSHARE,actionPDF,actionEDIT])
return configuration
}
// leftAction Leading
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let leftAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("leftAction tapped")
success(true)
})
leftAction.image = UIImage(named: "")
leftAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [leftAction])
}
Here's something that works for me but I'm not sure if it's correct and will always work in future
final class ViewController: UITableViewController {
private let data: [String] = [
"1", "2", "3", "4", "5"
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
cell.detailTextLabel?.text = data[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion ) in
completion(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
}
extension ViewController: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
return []
}
}

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

Adding an image to UITableViewCell in edit mode isn't aligning properly

I am using the code below to try to get the this look:
but instead it looks like:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete") { action, indexPath in
}
deleteRowAction.backgroundColor = UIColor.red
deleteRowAction.backgroundColor = UIColor(patternImage: UIImage(named: "DeleteIcon.png")!)
return [deleteRowAction]
}
What am I doing wrong?
Just change:
deleteRowAction.backgroundColor = UIColor(patternImage: UIImage(named: "DeleteIcon.png")!)
to:
deleteRowAction.image = UIImage(named: "DeleteIcon.png")
And it should work.
In below IOS 11 , editActionsForRowAt methods show the patter image on in background, and pattern image is repeated if image is small then cell size.
That's why use the third party for implement this SwipeCellKit
And if you implement this in IOS 11 then use this UITableViewDelegate function, It work perfectly Example:
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 = #imageLiteral(resourceName: "Delete")
deleteAction.backgroundColor = .red
let value = UISwipeActionsConfiguration(actions: [deleteAction])
value.performsFirstActionWithFullSwipe = true
return value
}

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