Skip to content

Commit

Permalink
Add support for more modern (non-deprecated) table view swipe API
Browse files Browse the repository at this point in the history
UITableViewRowAction and it’s associated methods have been deprecated in favor of the new UISwipeActionsConfiguration object which can be configured on the trailing and leading edges of a table view cell.

This API is available on iOS 11.0 onwards. The cleanest way to support this is to bump the deployment target to iOS 11.
  • Loading branch information
cgossain committed Jun 9, 2021
1 parent a57c21c commit 7610c5b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Static.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
Pod::Spec.new do |spec|
spec.name = 'Static'
spec.version = '4.0.2'
spec.version = '4.1.0'
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.platform = :ios, '8.0'
spec.ios.deployment_target = '11.0'
spec.frameworks = 'UIKit'
spec.source_files = 'Static/*.{swift,h}'
end
16 changes: 16 additions & 0 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ extension DataSource: UITableViewDataSource {
return rowAction
}
}

public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return row(at: indexPath)?.leadingSwipeActionsConfiguration.map {
let actionsConfiguration = UISwipeActionsConfiguration(actions: $0.actions)
actionsConfiguration.performsFirstActionWithFullSwipe = $0.performsFirstActionWithFullSwipe
return actionsConfiguration
}
}

public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return row(at: indexPath)?.trailingSwipeActionsConfiguration.map {
let actionsConfiguration = UISwipeActionsConfiguration(actions: $0.actions)
actionsConfiguration.performsFirstActionWithFullSwipe = $0.performsFirstActionWithFullSwipe
return actionsConfiguration
}
}

public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
guard let sectionIndexTitles = sectionIndexTitles, sectionIndexTitles.count >= sections.count else { return nil }
Expand Down
21 changes: 20 additions & 1 deletion Static/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ public struct Row: Hashable, Equatable {
self.selection = selection
}
}

/// Representation of the set of actions to perform when swiping on rows of a table.
public struct SwipeActionsConfiguration {
/// The swipe actions.
public let actions: [UIContextualAction]

/// A Boolean value indicating whether a full swipe automatically performs the first action.
public var performsFirstActionWithFullSwipe: Bool = true

public init(actions: [UIContextualAction]) {
self.actions = actions
}
}

// MARK: - Properties

Expand Down Expand Up @@ -137,9 +150,15 @@ public struct Row: Hashable, Equatable {

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

/// Returns the swipe actions to display on the leading edge of the row.
public var leadingSwipeActionsConfiguration: SwipeActionsConfiguration?

/// Returns the swipe actions to display on the trailing edge of the row.
public var trailingSwipeActionsConfiguration: SwipeActionsConfiguration?

var canEdit: Bool {
return editActions.count > 0
return (editActions.count > 0) || (leadingSwipeActionsConfiguration != nil) || (trailingSwipeActionsConfiguration != nil)
}

var isSelectable: Bool {
Expand Down

0 comments on commit 7610c5b

Please sign in to comment.