xiongxiong / ActionCell

ActionCell, wraps UITableViewCell with actions elegantly, no need to inherit UITableViewCell, use swiping to trigger actions (known from the Mailbox App).
MIT License
44 stars 14 forks source link

ActionCell

CI Status Version Platform Swift 3.0 Carthage compatible License

ActionCell, wraps UITableViewCell with actions, no need to inherit UITableViewCell, use swiping to trigger actions (known from the Mailbox App). I love it.

ActionCell

Contents

Features

Requirements

Attention

From version 2.0, the ActionCell framework redesigned, so API is different from version 2. to 1., if you used old version before, you must do some change, but not much.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate ActionCell into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'ActionCell', '~> 2.0.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate ActionCell into your Xcode project using Carthage, specify it in your Cartfile:

github "xiongxiong/ActionCell" ~> 2.0.0

Run carthage update to build the framework and drag the built ActionCell.framework into your Xcode project.

Manually

If you prefer not to use either of the dependency managers, you can integrate ActionCell into your project manually.

Example

Open the example project, build and run.

Protocols

ActionCellDelegate

public protocol ActionCellDelegate: NSObjectProtocol {

    var tableView: UITableView! { get }
    /// Do something when action triggered
    func didActionTriggered(cell: UITableViewCell, action: String)
}
public protocol ActionControlDelegate: NSObjectProtocol {
    func didActionTriggered(action: String)
}
public protocol ActionSheetDelegate: NSObjectProtocol {
    /// Setup action sheet
    func setupActionsheet(side: ActionSide, actions: [ActionControl])
    /// Open action sheet
    func openActionsheet(side: ActionSide, completionHandler: (() -> ())?)
    /// Close action sheet
    func closeActionsheet(_ completionHandler: (() -> ())?)
}

Usage

extension ViewController: ActionCellDelegate {

    public func didActionTriggered(cell: UITableViewCell, action: String) {
        ...
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "reuseId")!
              ... (cell customization)
              // create wrapper
              let wrapper = ActionCell()
              // set delegate
              wrapper.delegate = self
              // set animationStyle
              wrapper.animationStyle = .ladder
              // wrap cell with actions
              wrapper.wrap(cell: cell,
                           actionsLeft: [
                              {
                                  let action = IconTextAction(action: "cell 0 -- left 0")
                                  action.icon.image = #imageLiteral(resourceName: "image_5").withRenderingMode(.alwaysTemplate)
                                  action.icon.tintColor = UIColor.white
                                  action.label.text = "Hello"
                                  action.label.font = UIFont.systemFont(ofSize: 12)
                                  action.label.textColor = UIColor.white
                                  action.backgroundColor = UIColor(red:0.14, green:0.69, blue:0.67, alpha:1.00)
                                  return action
                              }(),
                              {
                                  let action = TextAction(action: "cell 0 -- left 1")
                                  action.label.text = "Long Sentence"
                                  action.label.font = UIFont.systemFont(ofSize: 12)
                                  action.label.textColor = UIColor.white
                                  action.backgroundColor = UIColor(red:1.00, green:0.78, blue:0.80, alpha:1.00)
                                  return action
                              }(),
                              {
                                  let action = IconAction(action: "cell 0 -- left 2")
                                  action.icon.image = #imageLiteral(resourceName: "image_0").withRenderingMode(.alwaysTemplate)
                                  action.icon.tintColor = UIColor.white
                                  action.backgroundColor = UIColor(red:0.51, green:0.83, blue:0.73, alpha:1.00)
                                  return action
                              }(),
                              ],
                           actionsRight: [
                              {
                                  let action = IconTextAction(action: "cell 0 -- right 0")
                                  action.icon.image = #imageLiteral(resourceName: "image_1").withRenderingMode(.alwaysTemplate)
                                  action.icon.tintColor = UIColor.white
                                  action.label.text = "Hello"
                                  action.label.font = UIFont.systemFont(ofSize: 12)
                                  action.label.textColor = UIColor.white
                                  action.backgroundColor = UIColor(red:0.14, green:0.69, blue:0.67, alpha:1.00)
                                  return action
                              }(),
                              {
                                  let action = TextAction(action: "cell 0 -- right 1")
                                  action.label.text = "Long Sentence"
                                  action.label.font = UIFont.systemFont(ofSize: 12)
                                  action.label.textColor = UIColor.white
                                  action.backgroundColor = UIColor(red:0.51, green:0.83, blue:0.73, alpha:1.00)
                                  return action
                              }(),
                              {
                                  let action = IconAction(action: "cell 0 -- right 2")
                                  action.icon.image = #imageLiteral(resourceName: "image_2").withRenderingMode(.alwaysTemplate)
                                  action.icon.tintColor = UIColor.white
                                  action.backgroundColor = UIColor(red:1.00, green:0.78, blue:0.80, alpha:1.00)
                                  return action
                              }(),
                              ])
              return cell
}

! CAUTION : To make UITableViewCell work properly on reuse, you should override the prepareForReuse method, and call clearActionsheet method:

override func prepareForReuse() {
        super.prepareForReuse()

        clearActionsheet() // remove actionCell view from cell
    }

Inherit ActionControl [Optional]

IconAction, TextAction & IconTextAction are already implemented, you can use it straightforwardly, or you can choose to use ActionControlDelegate to create your own ActionControl.

Properties & Methods

ActionCell

var animationStyle: AnimationStyle = ladder | ladder_emergence | concurrent // Action animation style
var animationDuration: TimeInterval = 0.3 // duration of the animation
var enableDefaultAction: Bool // Enable default action trigger when the content panned to far enough, if true, the first action (on left: the leftest one, on right: the rightest one) will be the default action.
var defaultActionTriggerPropotion: CGFloat // The propotion of (state public to state trigger-prepare / state public to state trigger), about where the default action triggered

func wrap(cell target: UITableViewCell, actionsLeft: [ActionControl] = [], actionsRight: [ActionControl] = [])

IconAction

init(action: String, width: CGFloat = 80, iconSize: CGSize = CGSize(width: 20, height: 20))

TextAction

init(action: String, width: CGFloat = 80)

IconTextAction

init(action: String, width: CGFloat = 80, iconSize: CGSize = CGSize(width: 20, height: 20), space: CGFloat = 5, offset: CGFloat = -3)

UITableViewCell

var isActionSheetOpened: Bool

func setupActionsheet(side: ActionSide, actions: [ActionControl] = []) // Change actionsheet's actions
func openActionsheet(side: ActionSide, completionHandler: (() -> ())? = nil)
func closeActionsheet(_ completionHandler: (() -> ())? = nil)

Author

xiongxiong, ximengwuheng@163.com

License

ActionCell is available under the MIT license. See the LICENSE file for more info.