Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Swift 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Sep 21, 2016
1 parent 123be89 commit 969dd7e
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 323 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SwipeableTableViewCell is a quite easy-to-use UITableViewCell subclass which all

##How To Get Started
###Carthage
Specify "SwipeableTableViewCell" in your Cartfile:
Specify "SwipeableTableViewCell" in your ```Cartfile```:
```ogdl
github "teambition/SwipeableTableViewCell"
```
Expand All @@ -18,22 +18,22 @@ import SwipeableTableViewCell
```
Configure cell in the data source like this:
```swift
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? SwipeableTableViewCell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? SwipeableTableViewCell
if cell == nil {
cell = SwipeableTableViewCell(style: .Default, reuseIdentifier: "Cell")
cell = SwipeableTableViewCell(style: .default, reuseIdentifier: "Cell")
}

// assign delegate if needed
cell!.delegate = self

// configure cell swipe actions
let deleteTitle = NSAttributedString(string: "删除", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)])
var deleteAction = SwipeableCellAction(title: deleteTitle, image: UIImage(named: "delete-icon"), backgroundColor: UIColor.redColor()) { Void in
let deleteTitle = NSAttributedString(string: "删除", attributes: [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 15)])
var deleteAction = SwipeableCellAction(title: deleteTitle, image: UIImage(named: "delete-icon"), backgroundColor: UIColor.red) { _ in
// do something when "deleteAction" is selected
}
let laterTitle = NSAttributedString(string: "稍后处理", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)])
var laterAction = SwipeableCellAction(title: laterTitle, image: UIImage(named: "later-icon"), backgroundColor: UIColor.blueColor()) { Void in
let laterTitle = NSAttributedString(string: "稍后处理", attributes: [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 15)])
var laterAction = SwipeableCellAction(title: laterTitle, image: UIImage(named: "later-icon"), backgroundColor: UIColor.blue) { _ in
// do something when "laterAction" is selected
}
deleteAction.width = 100
Expand All @@ -51,19 +51,19 @@ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:

##### 2. Implement delegate if needed
```swift
func swipeableCell(cell: SwipeableTableViewCell, scrollingToState state: SwipeableCellState) {
func swipeableCell(_ cell: SwipeableTableViewCell, isScrollingToState state: SwipeableCellState) {
// do something
}

func swipeableCellSwipeEnabled(cell: SwipeableTableViewCell) -> Bool {
func swipeableCellSwipeEnabled(_ cell: SwipeableTableViewCell) -> Bool {
// cell swipe enabled or not, default value is true
}

func allowMultipleCellsSwipedSimultaneously() -> Bool {
// allow multiple cells swiped simultaneously or not, default value is false
}

func swipeableCellDidEndScroll(cell: SwipeableTableViewCell) {
func swipeableCellDidEndScroll(_ cell: SwipeableTableViewCell) {
// do something
}
```
Expand All @@ -78,4 +78,4 @@ iOS 8.0
SwipeableTableViewCell is released under the MIT license. See [LICENSE](https://github.com/teambition/SwipeableTableViewCell/blob/master/LICENSE.md) for details.

## More Info
Have a question? Please [open an issue](https://github.com/teambition/SwipeableTableViewCell/issues/new)!
Have a question? Please [open an issue](https://github.com/teambition/SwipeableTableViewCell/issues/new)!
12 changes: 11 additions & 1 deletion SwipeableTableViewCell.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@
D3C6AD4F1C1FBE9600580522 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = Teambition;
TargetAttributes = {
D3C6AD571C1FBE9600580522 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -165,8 +166,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -213,8 +216,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -234,6 +239,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -245,6 +251,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -256,13 +263,15 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
D3C6AD621C1FBE9600580522 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -273,6 +282,7 @@
PRODUCT_BUNDLE_IDENTIFIER = Teambition.SwipeableTableViewCell;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion SwipeableTableViewCell/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.0.9</string>
<string>0.1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
4 changes: 2 additions & 2 deletions SwipeableTableViewCell/SwipeableCellAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public struct SwipeableCellAction {
public var title: NSAttributedString?
public var image: UIImage?
public var backgroundColor: UIColor?
public var action: (Void) -> Void
public var action: () -> ()
public var width: CGFloat = kSwipeableCellActionDefaultWidth
public var verticalSpace: CGFloat = kSwipeableCellActionDefaultVerticalSpace

public init(title: NSAttributedString?, image: UIImage?, backgroundColor: UIColor, action: (Void) -> Void) {
public init(title: NSAttributedString?, image: UIImage?, backgroundColor: UIColor, action: @escaping () -> ()) {
self.title = title
self.image = image
self.backgroundColor = backgroundColor
Expand Down
78 changes: 39 additions & 39 deletions SwipeableTableViewCell/SwipeableCellActionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ import UIKit
let kActionItemViewTag = 1000

class ActionItemView: UIView {
private(set) var title: NSAttributedString?
private(set) var image: UIImage?
private(set) var width: CGFloat = 0
private(set) var verticalSpace: CGFloat = 0
private(set) var index = 0
private(set) var action: ((Void)-> Void)!
fileprivate(set) var title: NSAttributedString?
fileprivate(set) var image: UIImage?
fileprivate(set) var width: CGFloat = 0
fileprivate(set) var verticalSpace: CGFloat = 0
fileprivate(set) var index = 0
fileprivate(set) var action: (()-> ())!

lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .Center
imageView.backgroundColor = UIColor.clearColor()
imageView.userInteractionEnabled = false
imageView.contentMode = .center
imageView.backgroundColor = UIColor.clear
imageView.isUserInteractionEnabled = false
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textAlignment = .Center
titleLabel.backgroundColor = UIColor.clearColor()
titleLabel.userInteractionEnabled = false
titleLabel.textAlignment = .center
titleLabel.backgroundColor = UIColor.clear
titleLabel.isUserInteractionEnabled = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
return titleLabel
}()

convenience init(action: SwipeableCellAction, index: Int) {
self.init(frame: CGRectZero)
self.init(frame: CGRect.zero)
tag = kActionItemViewTag
translatesAutoresizingMaskIntoConstraints = false

Expand All @@ -48,57 +48,57 @@ class ActionItemView: UIView {
self.index = index
self.action = action.action

userInteractionEnabled = true
isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ActionItemView.didTapActionItemView(_:)))
tapGesture.numberOfTapsRequired = 1
addGestureRecognizer(tapGesture)

if let image = image, title = title {
if let image = image, let title = title {
imageView.image = image
titleLabel.attributedText = title
let contentView = UIView()
contentView.backgroundColor = UIColor.clearColor()
contentView.backgroundColor = UIColor.clear
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.userInteractionEnabled = false
contentView.isUserInteractionEnabled = false
contentView.addSubview(imageView)
contentView.addSubview(titleLabel)
addSubview(contentView)
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options: [], metrics: nil, views: ["contentView": contentView]))
addConstraint(NSLayoutConstraint(item: contentView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[titleLabel]|", options: [], metrics: nil, views: ["titleLabel": titleLabel]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[imageView]-\(verticalSpace)-[titleLabel]|", options: [], metrics: nil, views: ["imageView": imageView, "titleLabel": titleLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: [], metrics: nil, views: ["contentView": contentView]))
addConstraint(NSLayoutConstraint(item: contentView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1, constant: 0))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[titleLabel]|", options: [], metrics: nil, views: ["titleLabel": titleLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageView]-\(verticalSpace)-[titleLabel]|", options: [], metrics: nil, views: ["imageView": imageView, "titleLabel": titleLabel]))
} else if let image = image {
imageView.image = image
addSubview(imageView)
addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
} else if let title = title {
titleLabel.attributedText = title
addSubview(titleLabel)
addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[titleLabel]|", options: [], metrics: nil, views: ["titleLabel": titleLabel]))
addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[titleLabel]|", options: [], metrics: nil, views: ["titleLabel": titleLabel]))
}
}

func didTapActionItemView(sender: AnyObject) {
func didTapActionItemView(_ sender: Any) {
action()
}
}

class SwipeableCellActionsView: UIView {
private(set) var cell: SwipeableTableViewCell?
private(set) var actionItemViews = [ActionItemView]()
private var actionItemViewBackgroundColors = [UIColor]()
fileprivate(set) var cell: SwipeableTableViewCell?
fileprivate(set) var actionItemViews = [ActionItemView]()
fileprivate var actionItemViewBackgroundColors = [UIColor]()

convenience init(actions: [SwipeableCellAction]?, parentCell: SwipeableTableViewCell) {
self.init(frame: CGRectZero)
self.init(frame: CGRect.zero)
translatesAutoresizingMaskIntoConstraints = false
self.cell = parentCell
setActions(actions)
}

func setActions(actions: [SwipeableCellAction]?) {
func setActions(_ actions: [SwipeableCellAction]?) {
func resetData() {
for subview in subviews {
if subview.tag == kActionItemViewTag {
Expand All @@ -108,7 +108,7 @@ class SwipeableCellActionsView: UIView {
}
actionItemViews.removeAll()
}
func validActions(actions: [SwipeableCellAction]?) -> Bool {
func validActions(_ actions: [SwipeableCellAction]?) -> Bool {
if let actions = actions {
return actions.count > 0
}
Expand All @@ -120,21 +120,21 @@ class SwipeableCellActionsView: UIView {
return
}

for (index, action) in actions!.enumerate() {
for (index, action) in actions!.enumerated() {
let actionItemView = ActionItemView(action: action, index: index)
actionItemViews.append(actionItemView)
}
var horizontalFormat = String()
var itemViews = [String: ActionItemView]()
for (index, actionItemView) in actionItemViews.reverse().enumerate() {
for (index, actionItemView) in actionItemViews.reversed().enumerated() {
let itemViewString = "actionItemView\(index)"
addSubview(actionItemView)
addConstraint(NSLayoutConstraint(item: actionItemView, attribute: .Width, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: actionItemView.width))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(itemViewString)]|", options: [], metrics: nil, views: [itemViewString: actionItemView]))
addConstraint(NSLayoutConstraint(item: actionItemView, attribute: .width, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: actionItemView.width))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[\(itemViewString)]|", options: [], metrics: nil, views: [itemViewString: actionItemView]))
horizontalFormat += "[\(itemViewString)]"
itemViews.updateValue(actionItemView, forKey: itemViewString)
}
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|\(horizontalFormat)|", options: [], metrics: nil, views: itemViews))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|\(horizontalFormat)|", options: [], metrics: nil, views: itemViews))
}

func pushBackgroundColors() {
Expand All @@ -145,7 +145,7 @@ class SwipeableCellActionsView: UIView {
}

func popBackgroundColors() {
for (index, color) in actionItemViewBackgroundColors.enumerate() {
for (index, color) in actionItemViewBackgroundColors.enumerated() {
let actionItemView = actionItemViews[index]
actionItemView.backgroundColor = color
}
Expand Down
Loading

0 comments on commit 969dd7e

Please sign in to comment.