Code Monkey home page Code Monkey logo

ex-uikit-uitableview-customrowactions's Introduction

UITableView and custom edit actions

This sample project shows how to provide custom actions on cells.

Idea is to replace the standar "Delete" button that appear on right of cell when user swipes to the left, like on the image bellow:

Custom action on table cell

Content

This project is written in Swift. You'll find two classes:

  1. The application delegate, as generated by the Xcode templates.
  2. The table view controller StaticTable. This object is also the table delegate and it's data source.
  3. Application UI is specified in a storyboard file.

Enabling swipe to left action

The data source must implement the method

func tableView(
	tableView: UITableView, 
	commitEditingStyle editingStyle: UITableViewCellEditingStyle, 
	forRowAtIndexPath indexPath: NSIndexPath
	)
{
	// Don't do anything here, unless you implement a delete or move action
}

Providing custom actions

This part of the job is implemented by the table view delegate.

You need to implement the method:

override func tableView(
	tableView: UITableView,
	editActionsForRowAtIndexPath indexPath: NSIndexPath
	) -> [AnyObject]?
{
	return rowActions
}

The rowActions value returned is a variable in the delegate object.

var rowActions = [ UITableViewRowAction ]()

In the example it is initialized when view is ready to be displayed.

override internal func viewDidLoad()
{
	// ... do stuff before
	
	// Build a list of actions for cells
	
	// Create an action
	var blueAction = UITableViewRowAction(style: .Normal, title: "Blue", handler: {
            (action, indexPath) in
            NSLog("Perform action \( action.title )")
        })
    
    // And perform some basic init    
	blueAction.backgroundColor = UIColor.blueColor()
        
	rowActions.append(blueAction)
	// ...
}

The action cal be implemented of called in the handler closure specified when the action object is created.

ex-uikit-uitableview-customrowactions's People

Contributors

sylvaingml avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.