ActionCell 2.0.9

ActionCell 2.0.9

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Wonder Bear.



  • By
  • xiongxiong

ActionCell

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

  • [x] Flexible, No need to inherit UITableViewCell
  • [x] Easy to use
  • [x] Customizable action control
  • [x] Support default action

Requirements

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

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

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

  • ActionCellDelegate
extension ViewController: ActionCellDelegate {

    public func didActionTriggered(cell: UITableViewCell, action: String) {
        ...
    }
}
  • Wrap your UITableViewCell with ActionCell
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, [email protected]

License

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