Change "Delete Text" on Swipe Eureka - ios

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.

Related

tableview display more option on `didSelect` event

How to display the uitableview's swipe option on didselectRow:
want to display swipe option like below.
exmaple show in fig, this should implement in didselect row. How can achive this help me. Thanks advance.
First , you should sure that:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
and second you will implementate in this delegate method:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
println("more button tapped")
}
more.backgroundColor = UIColor.lightGrayColor()
let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
println("favorite button tapped")
}
favorite.backgroundColor = UIColor.orangeColor()
let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
println("share button tapped")
}
share.backgroundColor = UIColor.blueColor()
return [share, favorite, more]
}
the finall view like that:
hope this is helpful for you.

Tableview doesn't edit on swipe action

I'm trying to implement the ability to edit the order of the tableview cells in my app. To do this, the user swipes right and taps on an edit button. However, when the edit button is tapped, the tableview doens't begin editing.
Below is my code:
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let edit = UIContextualAction(style: .normal, title: "Edit") { (UIContextualAction, UIView, complete: # escaping (Bool) -> Void) in
self.toggleTableViewEditing()
self.editEnabled()
complete(true)
}
return UISwipeActionsConfiguration(actions: [edit])
}
func editEnabled() {
if tableView.isEditing == true {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(toggleTableViewEditing))
} else {
navigationItem.rightBarButtonItem = nil
}
}
#objc func toggleTableViewEditing() {
tableView.isEditing = !tableView.isEditing
editEnabled()
}
Once the edit button is tapped, the edit button dismisses but the tableview doesn't enter the .isEditing state like it should.
You can use editActionsForRowAt function :
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .default, title: "Edit") { (action, index) in
// your edit action here
}
return [editAction]
}
I made an app like this and its work well.

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

How to edit the content of a Table Cell in Swift 3

I am a beginner at Swift 3. I have a Table View, and the user can delete a table view cell. Now I want the user to be able to change the content of the cell. I have an array that contains four names ["Stremmel", "Emma", "Sam", "Daisy"] and I want the user to be able to say edit Stremmel to George.
I searched for documentation or a similar question that could help me to figure a way to do so, but I got more confused. Can someone please provide me with some help!! Thank you. Here is my table view:
import UIKit
var list = ["Stremmel" , "Emma" , "Sam" , "Daisy"]
class ViewController: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = list[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
list.remove(at: indexPath.row)
tableView.reloadData()
}
}
If you want to show Edit button also with Delete button then you need to implement editActionsForRowAt method with canEditRowAt method instead of commit editingStyle.
After that with editActionsForRowAt show AlertController with textField and update its value and reload the row. So remove or comment the commit editingStyle method from your code and add below two methods.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
let alert = UIAlertController(title: "", message: "Edit list item", preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField) in
textField.text = self.list[indexPath.row]
})
alert.addAction(UIAlertAction(title: "Update", style: .default, handler: { (updateAction) in
self.list[indexPath.row] = alert.textFields!.first!.text!
self.tableView.reloadRows(at: [indexPath], with: .fade)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: false)
})
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in
self.list.remove(at: indexPath.row)
tableView.reloadData()
})
return [deleteAction, editAction]
}
Normally when creating a UITableViewController() class, you should have some template code that provides a edit-button and a delete-function (should be included in the edit button)! Just uncomment it, it should be accessible then!
or you could just call self.editButtonItem() in the viewDidLoad()-function.
I'm sorry for my bad English, I hope that answered you question!

Resources