45

Swipe Table View Cell for Custom Actions iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/QfMJjyv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The UITableViewRowAction class can be used to create custom actions to the rows of a Table View. In this Tutorial a rate and share action will be added to the Table View row. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

JvemEvE.png!web

For product name, use IOSActionsTableViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

aAvM7fe.png!web

Go to the storyboard . Remove the View Controller from the Storyboard and drag a Navigation Controller to the Scene. This will also add the Table View Controller . Select the Navigation Controller and go to The Attribute inspector. In the View Controller section check the "Is Initial View Controller" checkbox.

e6Vb6na.png!web

Select the Table View Cell and go to the Attributes Inspector. In the Table View Cell section set the Identifier  to " Cell" .

b6fQZf2.png!web

The storyboard will look like this.

NF3Y7vY.png!web

Since the View Controller is removed from the Storyboard the ViewController.swift file can also be deleted from the project. Add a new file to the project, select iOS->Source->Cocoa Touch Class. Name it TableViewController and make it a subclass of UITableViewController.

mM3eAvv.png!web

The TableViewController class needs to be linked to The Table View Controller object in the Storyboard. Select it and go the Identity Inspector. In the Custom Class section change the class to TableViewController.

BJBRVnA.png!web

Go to TableViewController.swift and create an array containing the table data.

let apps = ["Minecraft","Facebook","Tweetbot","Instagram"]

he TableViewController class contains some boilerplate code. Change the following delegate methods

override func numberOfSections(in tableView: UITableView) -> Int {
    // 1
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // 2
    return apps.count
}

  
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // 3
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = apps[indexPath.row]

    return cell
}
  1. There is only one section in the Table View so 1 needs to be returned in the numberOfSections(in:) method.

  2. The number of rows is equal to the number of items in the apps array so the count property of the array class is used.

  3. The name of the app at the current index of the apps array is assigned to the text property of the textLabel property of the current cell.

Build and Runthe project. The rows should be filled with the app's names.

fEn6NvA.png!web

To enable the custom actions when the user swipes on the Table View Cell, the tableView(_:editActionsForRowAt:) must be implemented.

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
    // 1
    let shareAction = UITableViewRowAction(style: .default, title: "Share" , handler: { (action:UITableViewRowAction, indexPath: IndexPath) -> Void in
    // 2
    let shareMenu = UIAlertController(title: nil, message: "Share using", preferredStyle: .actionSheet)
            
    let twitterAction = UIAlertAction(title: "Twitter", style: .default, handler: nil)
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            
    shareMenu.addAction(twitterAction)
    shareMenu.addAction(cancelAction)
            
    self.present(shareMenu, animated: true, completion: nil)
    })
    // 3
    let rateAction = UITableViewRowAction(style: .default, title: "Rate" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in
    // 4
    let rateMenu = UIAlertController(title: nil, message: "Rate this App", preferredStyle: .actionSheet)
            
    let appRateAction = UIAlertAction(title: "Rate", style: .default, handler: nil)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            
    rateMenu.addAction(appRateAction)
    rateMenu.addAction(cancelAction)
             
    self.present(rateMenu, animated: true, completion: nil)
  })
    // 5
    return [shareAction,rateAction]
}
  1. A TableViewRowAction defines the Share action to present

  2. An Alert Controller is presented with the Twitter and cancel actions

  3. A TableViewRowAction defines the Rate action to present

  4. An Alert Controller is presented with the Rate and cancel actions

  5. This method returns an array of UITableViewRowAction objects.

Build and Runthe project and swipe to see the custom defined actions.

6BRjI3b.png!web

You can download the source code of the IOSActionsTableViewTutorial at the ioscreator repository on Github


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK