Created
December 22, 2014 12:09
-
-
Save flexaddicted/2919d4ef6b7c50bebc24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
@IBOutlet var tableView: UITableView! | |
private var swipeGestureStarted: Bool = false | |
private var selectedIndexPath: NSIndexPath? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 2 | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MyCell") as UITableViewCell | |
cell.textLabel?.text = "Swipe to delete row" | |
return cell | |
} | |
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
self.selectedIndexPath = indexPath | |
} | |
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { | |
return true | |
} | |
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { | |
if(editingStyle == .Delete) { | |
self.swipeGestureStarted = false | |
// Here you should commit your editing | |
} | |
} | |
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { | |
return UITableViewCellEditingStyle.Delete; | |
} | |
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) { | |
self.swipeGestureStarted = true; | |
} | |
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) { | |
if(self.swipeGestureStarted) { | |
self.swipeGestureStarted = false | |
self.tableView.selectRowAtIndexPath(self.selectedIndexPath, animated: true, scrollPosition: .None); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment