GithubHelp home page GithubHelp logo

supergeroy / lprtableview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nicolasgomollon/lprtableview

0.0 0.0 0.0 95 KB

A drop-in replacement for UITableView and UITableViewController that supports long-press reordering of cells.

License: MIT License

Ruby 1.96% Swift 98.04%

lprtableview's Introduction

LPRTableView

LPRTableView (LPR is short for “Long Press to Reorder”) is a drop-in replacement for UITableView and UITableViewController that supports reordering by simply long-pressing on a cell. LPRTableView is written completely in Swift (adapted from Objective-C, original code by: bvogelzang/BVReorderTableView).

Sample Screenshot

Usage

Simply replace the UITableView of your choice with LPRTableView, or replace UITableViewController with LPRTableViewController. That’s it!

It’s important that you update your data source after the user reorders a cell:

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
	// Modify this code as needed to support more advanced reordering, such as between sections.
	let source = objects[sourceIndexPath.row]
	let destination = objects[destinationIndexPath.row]
	objects[sourceIndexPath.row] = destination
	objects[destinationIndexPath.row] = source
}

It is possible to select which cells can be reordered by implementing the following optional standard UITableViewDataSource method (the absence of this method defaults to all cells being reorderable):

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
	// Change this logic to match your needs.
	return (indexPath.section == 0)
}

Long-press reordering can be disabled entirely by setting a Bool to lprTableView.longPressReorderEnabled.

There are also a few optional delegate methods you may implement after setting lprTableView.longPressReorderDelegate:

// Provides a chance to modify the cell (visually) before dragging occurs.
//    NOTE: Any changes made here should be reverted in `tableView:cellForRowAtIndexPath:`
//          to avoid accidentally reusing the modifications.
func tableView(_ tableView: UITableView, draggingCell cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) -> UITableViewCell {
	cell.backgroundColor = .green
	return cell
}

// Called within an animation block when the dragging view is about to show.
func tableView(_ tableView: UITableView, showDraggingView view: UIView, at indexPath: NSIndexPath)

// Called within an animation block when the dragging view is about to hide.
func tableView(_ tableView: UITableView, hideDraggingView view: UIView, at indexPath: NSIndexPath)

// Called when the dragging gesture's vertical location changes.
func tableView(_ tableView: UITableView, draggingGestureChanged gesture: UILongPressGestureRecognizer)

See the ReorderTest demo project included in this repository for a working example of the project, including the code above.

If you’re replacing UITableViewController with LPRTableViewController and are using a custom UITableViewCell subclass, then you must override registerClasses() and register the appropriate table view cell class(es) within this method. Do not call super within this method.

override func registerClasses() {
	tableView.register(MyCustomTableViewCell.self, forCellReuseIdentifier: "Cell")
}

Requirements

Since LPRTableView is written in Swift 3, it requires Xcode 8 or above and works on iOS 8 and above.

License

LPRTableView is released under the MIT License.

lprtableview's People

Contributors

nicolasgomollon avatar laptopmini avatar edwinfinch avatar hendx avatar pjh68 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.