Skip to content
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale here for only supporting #selector(NSObject.copy(_:)? Could we have supported other actions?

}

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about reverting this whitespace change at the beginning of the line? Agreed that this init takes in a lot of parameters and could use some better clarity, but it might be best to scope that out to a different PR. 😄


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