Skip to content

Commit

Permalink
Add support for copy action
Browse files Browse the repository at this point in the history
Closes venmo#80
  • Loading branch information
tomaskraina committed May 4, 2016
1 parent 77d7929 commit 3d7607c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class ViewController: TableViewController {
self.showAlert(title: "Deleted.")
})
])
]),
Section(header: "Copying", rows: [
Row(text: "Tap and hold this row", copyAction: { [unowned self] row in
self.showAlert(title: "Copied.")
})
])
]
}
Expand Down
14 changes: 14 additions & 0 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,18 @@ extension DataSource: UITableViewDelegate {
row.accessory.selection?()
}
}

public func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return rowForIndexPath(indexPath)?.canCopy ?? false
}

public func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return action == #selector(NSObject.copy(_:)) && (rowForIndexPath(indexPath)?.canCopy ?? false)
}

public func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
if let row = rowForIndexPath(indexPath) where action == #selector(NSObject.copy(_:)) {
row.copyAction?(row)
}
}
}
13 changes: 12 additions & 1 deletion Static/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import UIKit
/// Row or Accessory selection callback.
public typealias Selection = () -> Void

/// Row copy callback
public typealias CopyAction = (Row) -> Void

/// Representation of a table row.
public struct Row: Hashable, Equatable {

Expand Down Expand Up @@ -113,7 +116,14 @@ public struct Row: Hashable, Equatable {

/// Actions to show when swiping the cell, such as Delete.
public var editActions: [EditAction]

/// Action to run when the row is selected to copy
public var copyAction: CopyAction?

var canCopy: Bool {
return copyAction != nil
}

var canEdit: Bool {
return editActions.count > 0
}
Expand All @@ -134,7 +144,7 @@ public struct Row: Hashable, Equatable {
// MARK: - Initializers

public init(text: String? = nil, detailText: String? = nil, selection: Selection? = nil,
image: UIImage? = nil, accessory: Accessory = .None, cellClass: CellType.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], UUID: String = NSUUID().UUIDString) {
image: UIImage? = nil, accessory: Accessory = .None, cellClass: CellType.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], copyAction: CopyAction? = nil, UUID: String = NSUUID().UUIDString) {

self.UUID = UUID
self.text = text
Expand All @@ -145,6 +155,7 @@ public struct Row: Hashable, Equatable {
self.cellClass = cellClass ?? Value1Cell.self
self.context = context
self.editActions = editActions
self.copyAction = copyAction
}
}

Expand Down

0 comments on commit 3d7607c

Please sign in to comment.