Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for copy action #81

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 4 additions & 7 deletions Example/CustomTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Example/NibTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions Example/NibTableViewCell.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9060" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand All @@ -14,20 +19,19 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Custom" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0Kr-e3-EF9">
<rect key="frame" x="130" y="41" width="60" height="21"/>
<animations/>
<rect key="frame" x="130" y="30" width="60" height="43.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<animations/>
<constraints>
<constraint firstItem="0Kr-e3-EF9" firstAttribute="centerX" secondItem="H2p-sc-9uM" secondAttribute="centerX" id="0DV-UL-ORw"/>
<constraint firstItem="0Kr-e3-EF9" firstAttribute="centerY" secondItem="H2p-sc-9uM" secondAttribute="centerY" id="Ejd-1V-inO"/>
<constraint firstAttribute="bottom" secondItem="0Kr-e3-EF9" secondAttribute="bottom" constant="30" id="KNc-V8-rli"/>
<constraint firstItem="0Kr-e3-EF9" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="30" id="Lul-1i-KRr"/>
</constraints>
</tableViewCellContentView>
<animations/>
<connections>
<outlet property="centeredLabel" destination="0Kr-e3-EF9" id="BTw-PS-u6X"/>
</connections>
Expand Down
7 changes: 6 additions & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
]
}
Expand Down
4 changes: 2 additions & 2 deletions Static.podspec
Original file line number Diff line number Diff line change
@@ -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' => '[email protected]', 'Sam Soffes' => '[email protected]' }
spec.author = { 'Venmo' => '[email protected]', 'Sam Soffes' => '[email protected]', 'Tom Kraina' => '[email protected]' }

spec.platform = :ios, '8.0'
spec.frameworks = 'UIKit'
Expand Down
2 changes: 1 addition & 1 deletion Static.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
};
36748D4E1B5034EC0046F207 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0800;
LastSwiftMigration = 0820;
};
};
};
Expand Down
14 changes: 14 additions & 0 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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: 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
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