Custom swipe to delete horizontal button in TableViewCell - ios

I have managed to implement these two button which is native provided by iOS using editActionsForRowAt method but I need following output. Anyone help me how do I do this?
Following is the code I tried
func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let accept = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
print("accept button tapped")
self.animateAvailabilityHideShow(isHide: false, duration: 0.3)
}
accept.backgroundColor = UIColor().themeGreenColor
let reject = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
print("reject button tapped")
}
reject.backgroundColor = UIColor().themeRedColor
return [reject, accept]
}

Related

Swift: Swipe action to strikethrough row in TableView

Evening ladies and gentleman,
I am currently getting used to Swift and wanted to start with a little todo app. So far I can add an item and safe it persistently in a context. When an item has been added, it will be shown in a tableview. Now, I want to use a check swipe to strikethrough items, which have been added and safe this information in my context. Deleting using a swipe works perfectly fine.
Has anybody an idea how realize this? I tried to solve it by myself, but couldnt get it done. A similar question has been asked here before, but didnt get a proper answer: Add strikethrough to tableview row with a swipe
func checkAccessoryType(cell: UITableViewCell, isCompleted: Bool) {
if isCompleted {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let todo = CoreDataManager.shared.getTodoItem(index: indexPath.row)
todo.completed = !todo.completed
CoreDataManager.shared.safeContext()
if let cell = tableView.cellForRow(at: indexPath){
checkAccessoryType(cell: cell, isCompleted: todo.completed)
}
}
Assuming you are trying to strikethrough the title of your task -- which should be defined as a label -- here is the approach to take:
1- Make sure your label is set to attributed text rather than plain. To do that, go to Main.storyboard, select your label, and inside the attribute inspector, set text to Attributed.
2- Inside your completion block (that is the completion block executed after a swipe) add the following code:
(SWIFT 5)
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text)
attributeString.addAttribute(.strikethroughStyle, value: 1, range: NSRange(location: 0, length: taskLabel.text.count))
taskLabel.attributedText = attributeString
Just a little advice: it's always helpful if you add some code when you ask a question.
Let me know if anything doesn't make sense.
Looking at the link that you provided, you need swipe action on your UITableViewCell.
Try looking into:
leadingSwipeActionsConfigurationForRowAt
trailingSwipeActionsConfigurationForRowAt
You need this action to perform the strikethrough label or delete:
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])
}
Source: https://developerslogblog.wordpress.com/2017/06/28/ios-11-swipe-leftright-in-uitableviewcell/

SwipeCellKit action won't perform segue

I'm using SwipeCellKit for my TO DO List app. When the user swipes left it deletes the item, but when the user swipes right I want him to be able to set a reminder on this item, so I've created an actionset a reminder
this action should perform a segue which brings the user to a custom popup with a date picker in it. The problem is that when I click on the button to set a reminder the simulator quits with an uncaught exception. I've already tried to perform deletion from this button it works perfectly, I've also tried to perform another segue to another view controller from this button the simulator quits. Could someone tell me what I'm doing wrong here? Here's my code:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? { if orientation == .left {
guard isSwipeRightEnabled else { return nil }
let setReminder = SwipeAction(style: .default, title: "Set a reminder") { action, indexPath in
self.updateModelByAddingAReminder(at: indexPath)
}
setReminder.image = UIImage(named: "reminder-icon")
return[setReminder]
}else{
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.updateModel(at: indexPath)
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete-icon")
// return [setReminder, deleteAction]
return [deleteAction]
}
Ok, I found problem in your options for cell.
From doc
The built-in .destructive, and .destructiveAfterFill expansion styles are configured to automatically perform row deletion when the action handler is invoked (automatic fulfillment).
And you need use destructive style for cell in editActionsForRowAt. Or use another options, for example
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions {
var options = SwipeTableOptions()
options.transitionStyle = .border
if orientation == .left{
//or none
options.expansionStyle = .selection
}else{
options.expansionStyle = .destructive
}
return options
}
Hope it's help.

Tap on TableViewRowAction just disappears action buttons

I have a UITableView which in every call has two action.
When I tap on one of them their action not being called and just these action button disappears and cell comes back to its normal place.
their action just being called when I tap and hold for a long time!
how can I fix that?
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let cancel = UITableViewRowAction(style: .destructive, title: "cacel") { action, index in
print("c tapped")
}
let paymentType = UITableViewRowAction(style: .destructive, title: "patmentType") { action, index in
print("p tapped")
}
return [paymentType,cancel]
}
tableView deleagte is set.
I tried code below still bot working :
self.tableView.setEditing(false, animated: true)
and
tableView.isEditing = false
not working yet.
any help please

Swipe actions for row with iOS system icons instead of text

I've set up a destructive trailing swipe action under iOS 11:
#available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, completionHandler) in
self.deleteRow(atIndexPath: indexPath)
completionHandler(true)
})
// Is it possible here?
//deleteAction.image = UIImage(named: "delete")
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
}
Instead of the "delete" text, I'd like to show the Apple system icon thrash bin found here:
https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/system-icons/
Obviously, I could obtain the icon from somewhere and set it as the image, but I guess there should be a native way to do this.
Thanks for your suggestions!

iOS: UITableView row swipe from left for more options in Swift

I am using iOS 9 and Swift 2.2
How do I modify below function/add new function to make some options to swipe from left?
I want to add a few options to be shown on swiping from both left and right. I am sure we do not need any third party API to do this, but I want to be able to modify the below code to achieve the required functionality.
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let MorAknOpnVar = UITableViewRowAction(style: .Normal, title: "More")
{ action, index in
print("More OpnBtnKlk")
/* CodTdo */
}
MorAknOpnVar.backgroundColor = UIColor.lightGrayColor()
let NamAknOpnVar = UITableViewRowAction(style: .Normal, title: "NamTtl")
{ action, index in
print("NamTtl OpnBtnKlk")
/* CodTdo */
}
NamAknOpnVar.backgroundColor = UIColor.orangeColor()
let ShrAknOpnVar = UITableViewRowAction(style: .Normal, title: "Share")
{ action, index in
print("Share OpnBtnKlk")
/* CodTdo */
}
ShrAknOpnVar.backgroundColor = UIColor.blueColor()
return [ShrAknOpnVar, NamAknOpnVar, MorAknOpnVar]
}

Resources