diff --git a/Example/CustomTableViewCell.swift b/Example/CustomTableViewCell.swift index b4b8e4d..94ec75f 100644 --- a/Example/CustomTableViewCell.swift +++ b/Example/CustomTableViewCell.swift @@ -22,13 +22,10 @@ final class CustomTableViewCell: UITableViewCell, Cell { contentView.addSubview(centeredLabel) - NSLayoutConstraint.activate([ - centeredLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), - centeredLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 8), - centeredLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), - centeredLabel.leadingAnchor.constraint(greaterThanOrEqualTo: contentView.leadingAnchor, constant: 8), - centeredLabel.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -8) - ]) + let views = ["centeredLabel": centeredLabel] + var constraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(withVisualFormat: "|-[centeredLabel]-|", options: [], metrics: nil, views: views) + constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-[centeredLabel]-|", options: [], metrics: nil, views: views) + NSLayoutConstraint.activate(constraints) } required init?(coder aDecoder: NSCoder) { diff --git a/Example/NibTableViewCell.swift b/Example/NibTableViewCell.swift index 441acb8..46f70d2 100644 --- a/Example/NibTableViewCell.swift +++ b/Example/NibTableViewCell.swift @@ -5,7 +5,7 @@ final class NibTableViewCell: UITableViewCell, Cell { // MARK: - Properties - @IBOutlet weak private var centeredLabel: UILabel! + @IBOutlet weak fileprivate var centeredLabel: UILabel! // MARK: - CellType diff --git a/Example/NibTableViewCell.xib b/Example/NibTableViewCell.xib index cd832b5..998c327 100644 --- a/Example/NibTableViewCell.xib +++ b/Example/NibTableViewCell.xib @@ -1,7 +1,12 @@ - - + + + + + - + + + @@ -14,20 +19,19 @@ - + + - diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 6dc986c..c239ad7 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -26,7 +26,7 @@ class ViewController: TableViewController { title = "Static" - tableView.rowHeight = 50 + tableView.estimatedRowHeight = 60 // Note: // Required to be set pre iOS11, to support autosizing @@ -76,6 +76,11 @@ class ViewController: TableViewController { }) ]) ]), + Section(header: "Copying", rows: [ + Row(text: "Tap and hold this row", copyAction: { [unowned self] row in + self.showAlert(title: "Copied.") + }) + ]), Section(header: "AutoSized SectionFooterView", rows: [], footer: Section.Extremity.autoLayoutView(LargeAutoSizedExtremityView())) ] } diff --git a/Static.podspec b/Static.podspec index 6e60bdd..6483177 100644 --- a/Static.podspec +++ b/Static.podspec @@ -1,12 +1,12 @@ Pod::Spec.new do |spec| spec.name = 'Static' - spec.version = '2.3.0' + spec.version = '4.0.0.1' spec.summary = 'Simple static table views for iOS in Swift.' spec.description = 'Static provides simple static table views for iOS in Swift.' spec.homepage = 'https://github.com/venmo/static' spec.license = { type: 'MIT', file: 'LICENSE' } spec.source = { git: 'https://github.com/venmo/Static.git', tag: "v#{spec.version}" } - spec.author = { 'Venmo' => 'ios@venmo.com', 'Sam Soffes' => 'sam@soff.es' } + spec.author = { 'Venmo' => 'ios@venmo.com', 'Sam Soffes' => 'sam@soff.es', 'Tom Kraina' => 'me@tomkraina.com' } spec.platform = :ios, '8.0' spec.frameworks = 'UIKit' diff --git a/Static.xcodeproj/project.pbxproj b/Static.xcodeproj/project.pbxproj index a065020..582e592 100644 --- a/Static.xcodeproj/project.pbxproj +++ b/Static.xcodeproj/project.pbxproj @@ -281,7 +281,7 @@ }; 36748D4E1B5034EC0046F207 = { CreatedOnToolsVersion = 7.0; - LastSwiftMigration = 0800; + LastSwiftMigration = 0820; }; }; }; diff --git a/Static/DataSource.swift b/Static/DataSource.swift index 670ab25..b5d1199 100644 --- a/Static/DataSource.swift +++ b/Static/DataSource.swift @@ -304,6 +304,20 @@ extension DataSource: UITableViewDelegate { tableViewDelegate?.tableView?(tableView, accessoryButtonTappedForRowWith: indexPath) } + + public func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { + return row(at: indexPath)?.canCopy ?? false + } + + public func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool { + return action == #selector(UIResponder.copy(_:)) && (row(at: indexPath)?.canCopy ?? false) + } + + public func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) { + if let row = row(at: indexPath), action == #selector(UIResponder.copy(_:)) { + row.copyAction?(row) + } + } } extension UITableViewStyle { diff --git a/Static/Row.swift b/Static/Row.swift index c7c43ad..581c556 100644 --- a/Static/Row.swift +++ b/Static/Row.swift @@ -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 { @@ -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 } @@ -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: Cell.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], uuid: String = UUID().uuidString) { + image: UIImage? = nil, accessory: Accessory = .none, cellClass: Cell.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], copyAction: CopyAction? = nil, uuid: String = UUID().uuidString) { self.uuid = uuid self.text = text @@ -145,6 +155,7 @@ public struct Row: Hashable, Equatable { self.cellClass = cellClass ?? Value1Cell.self self.context = context self.editActions = editActions + self.copyAction = copyAction } }