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
}
Related
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.
Is there a way to change the text of the ContextualAction so it doesn't say "delete" anymore?
You can simply create delegate method, trailingSwipeActionsConfigurationForRowAt like below and customise it as you want:
extension YOURVIEWCONROLLER: UITableViewDelegate{
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let removeAction = UIContextualAction(style: .destructive, title: "Remove") { action, view, complete in
//Your code
Utility.main.showAlert(message: "Removed Pressed", title: "Alert")
complete(true)
}
doneAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [removeAction])
}
}
Or
In Core.swift file you need to edit its function.
I have had a UITableView working for several version of Swift, but with Swift 5, it started presenting 'delete' action on a left swipe.
One of the rows is 'swipe-able' (the 'Expanded' one) and others are not, so I return a nil in place of the RowAction.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
if listOfCurrentAwards[indexPath.row] != "Expanded" {
print(">>> Skipping row \(indexPath.row)")
return nil
} else {
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
self.prepareOneDogMessageShareSheet(row: indexPath.row)
}
share.backgroundColor = UIColor.blue
return [share]
}
}
I am also getting the same behavior with the older 'editActionsForRowAtIndexPath'.
Anyone else seeing the same thing? Did you find a work around?
For now I am just returning a dummy action that displays a dog related emoji ().
if listOfCurrentAwards[indexPath.row] != "Expanded" {
//TBD - remove the following line if the nill action is supported/fixed.
let dogEmoji = ["🐶","🐩","🦴","🐕","💩","🐾"]
let share = UITableViewRowAction(style: .normal, title: dogEmoji.randomElement()) { action, index in
print(">>> Skipping row \(indexPath.row)")
}
return [share] //nil
} else ...
Update 1
Even refactoring to use trailingSwipeActionsConfigurationForRowAt did not work, I'm getting the same result.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
if listOfCurrentAwards[indexPath.row] != "Expanded" {
print(">>> Swiping not enabled for row \(indexPath.row)")
return nil
} else {
print(">>> 'Share' swipped on \(indexPath.row)")
let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
print(">>> 'Share' clicked on \(indexPath.row)")
self.prepareOneDogMessageShareSheet(row: indexPath.row)
}
shareAction.backgroundColor = UIColor.blue
let configuration = UISwipeActionsConfiguration(actions: [shareAction])
configuration.performsFirstActionWithFullSwipe = true //false to not support full swipe
return configuration
}
}
Answer
I had to add the canEditRowAt helper, allow me to move some of the logic from trailingSwipeActionsConfigurationForRowAt.
func tableView(_ tableView: UITableViewbcgdfgdfg, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?vdfgh
{
print(">>> Trying to swipe row \(indexPath.row)")
let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
print(">>> 'Share' clicked on \(indexPath.row)")
self.prepareOneDogMessageShareSheet(row: indexPath.row)
}
shareAction.backgroundColor = UIColor.blue
let configuration = UISwipeActionsConfiguration(actions: [shareAction])
return configuration
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return listOfCurrentAwards[indexPath.row] == "Expanded"
}
editActionsForRowAt is outmoded if the goal is to control what happens when you swipe. So is UITableViewRowAction.
You should be using tableView(_:trailingSwipeActionsConfigurationForRowAt:).
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])
}
I have implemented left/right swiping for UITableViewCell
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view: UIView, success:(Bool) -> Void) in
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
let config = UISwipeActionsConfiguration(actions: [closeAction])
return config
}
override 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])
}
but cell remains on the last position when I release it after dragging some bit(even tough it moves back when I click it again ). Is there a way to move the cell back to initial position after dragging it some-bit and releasing? Thanks