Uitableviewcell swipe for custom button in iOS 9 Swift

 

This tutorial has been updated for X code 7.3 & swift 2.2

Uitableviewcell swipe was first introduced in iOS 7 mail application. But it was not available for everyone. In iOS 8 class UITableViewRowAction was introduced so you don’t have to create custom class for it.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to tableviewcell swipe demo & save it on desktop.

new x code project

 

2. Go to main storyboard & select view controller & delete it. Add tableview controller in main storyboard.

add table view controller

 

3. Select tableview controller & make it initial view controller

set initial view controller

 

4. Now delete view controller swift file & add new cocoa touch class file of class uitableviewcontroller

add table view controller swift file

 

5. Go to main storyboard; select table view controller & set its class to TableViewController

set custom class

 

6. Select prototype cell & set its identifier to tableCell

prototype cell identifier

 

7. Go to TableViewController swift file & create an array called appList as follows

class TableViewController: UITableViewController {
var appList = ["facebook", "twitter", "youtube", "instagram", "what's app"]

8. In view did load add following code to start table from below stat bar

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

9. Set number of sections to 1 & number of rows in section to appList.count

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return appList.count
}

10. Uncomment cellForRowAtIndexPath method & add following code

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

    // Configure the cell...
    cell.textLabel?.text = appList[indexPath.row]

    return cell
}

11. Now uncomment commitEditingStyle method & delete everything inside the curly brackets

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

}

12. Add editActionsForRowAtIndexPath method & insert following code

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let rateAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Rate") { (action , indexPath ) -> Void in
        self.editing = false
        print("Rate button pressed")
    }

    let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Share") { (action , indexPath) -> Void in
        self.editing = false
        print("Share button pressed")
    }
    return [rateAction, shareAction]
}

When you add self.editing = false or tableview.editing = false then tableviewcell will go back to normal position. Otherwise it would stay in that state after the button is pressed.
Now build and run app in iPhone 5s & swipe uitableviewcell to see share & rate buttons

tablecell swipe button
Download X code file
Challenge 1: Add 3 buttons with different background color

button with background color

 

Challenge 2: When you click on any button double check intention with action sheet

double check with alert view
Download Challenge Solution

Total 7 Votes
2

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?