3

I'm working on a todo list app, and now I can use these code to achieve swipe left to delete

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
        print("Delete button tapped")
    }
    delete.backgroundColor = UIColor.redColor()
    return [delete]
}

And I think I searched the WHOLE internet but none of those solutions can do the swipe right job and work fine on me at the same time. I'm using swift 2.0, Xcode 7 beta 5.

Anyone done this before?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
David
  • 1,660
  • 3
  • 21
  • 33
  • Do you mean, you want to swipe right to show other action(s) in the cell's left? – Siam Aug 21 '15 at 14:56
  • Yes, like mark as unread, etc. @Siam – David Aug 21 '15 at 14:57
  • possible duplicate of [Swipe to Delete and the "More" button (like in Mail app on iOS 7)](http://stackoverflow.com/questions/17254402/swipe-to-delete-and-the-more-button-like-in-mail-app-on-ios-7) – mattt Aug 21 '15 at 15:13

3 Answers3

2

Use commit editing style

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let thisTask = fetchedResultsController.objectAtIndexPath(indexPath) as! TaskModel

    if thisTask.completed ==  true {thisTask.completed = false}
    else {thisTask.completed = true}
    (UIApplication.sharedApplication().delegate as! AppDelegate).saveContext()
}

I'm using CoreData to store my information, and one of the things I'm storing is wether it's completed or not, when a use swipes right on a tableview it changes the completed to true, and I have two sections, one for uncompleted tasks and one for completed

Kashish Goel
  • 94
  • 1
  • 9
  • I removed `let thisTask = fetchedResultsController.objectAtIndexPath(indexPath) as! TaskModel if thisTask.completed == true {thisTask.completed = false} else {thisTask.completed = true} (UIApplication.sharedApplication().delegate as! AppDelegate).saveContext()` and it's not working – David Aug 22 '15 at 02:04
  • You need the objectAtIndexPath since that tells the program which one to delete – Kashish Goel Aug 22 '15 at 02:06
1

It think this project can help you further...

SBGestureTableView: Swift UITableView subclass that supports swiping rows ala Mailbox and long press to move rows.

If you don't want to use this class. You can see in there source code, you first need to figure out if the user is swiping from left-to-right or right-to-left. And go from there.

0

I've finally done this by successfully converted SBGestureTableView to swift 2.0 syntax and I published that on github.com/theniceboy/SBGestureTableView/tree/swift-2.0. But I still have to thank you guys!

David
  • 1,660
  • 3
  • 21
  • 33