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:
This project is written in Swift. You'll find two classes:
- The application delegate, as generated by the Xcode templates.
- The table view controller StaticTable. This object is also the table delegate and it's data source.
- Application UI is specified in a storyboard file.
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
}
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.