Giter Site home page Giter Site logo

ios-pdgesturetableview's Introduction

PDGestureTableView


PDGestureTableView

Features

  • Swipe cells to perform multiple actions.
  • Tap and hold to move cells.
  • A UIView can be set to be shown when there's no content on the table view.
  • Storyboards/Xibs & Autolayout fully compatible.
  • Block-driven. No silly delegates :)

CocoaPods

You can install PDGestureTableView through CocoaPods adding the following to your Podfile:

pod 'PDGestureTableView'

At a glance

Setting up actions for a cell

PDGestureTableViewCell has 4 possible actions:

  • firstLeftAction
  • secondLeftAction
  • firstRightAction
  • secondRightAction

This is how actions should be set:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [...]

    cell.firstLeftAction = [PDGestureTableViewCellAction
                            actionWithIcon:[UIImage imageNamed:@"icon"]
                            color:[UIColor greenColor]
                            fraction:0.25
                            didTriggerBlock:^(PDGestureTableView *gestureTableView, NSIndexPath*indexPath) {
                                // Action for first left action triggering.
                            }];

    cell.secondLeftAction = [PDGestureTableViewCellAction
                            actionWithIcon:[UIImage imageNamed:@"icon"]
                            color:[UIColor redColor]
                            fraction:0.7
                            didTriggerBlock:^(PDGestureTableView *gestureTableView, NSIndexPath*indexPath) {
                                // Action for second left action triggering.
                            }];

    return cell;
}
  • icon is the UIImage icon for that action.
  • color is the UIColor for the action highlight.
  • fraction specifies the fraction of the entire cell width where the action will be highlighted. For instance, if you specify 0.5 the action will highlight when the cell gets to the middle of the table width.
  • didTriggerBlock is the block that will execute when the user releases the cell.

Actions for didTriggerBlock

didTriggerBlock can contain any action you want, but besides the ones you use, you must use one of the following:

pushCellForIndexPath:completion + deleteCellForIndexPath:animation:completion

Usually, you'll use these two methods to delete a cell in a table view whose data source is being managed by a NSFetchedResultsController object.

The first method will push it to the edge of the table view, and the second one will be called in the NSFetchedResultsController delegate method. It'll work great for non-gestural deletions as well.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	[...]

	__unsafe_unretained typeof(self) weakSelf = self; // Get a weak reference of self so you can access it from didTriggerBlock without creating retain cycles.

	cell.firstLeftAction = [PDGestureTableViewCellAction
							actionWithIcon:[UIImage imageNamed:@"icon"]
							color:[UIColor greenColor]
							fraction:0.25
							didTriggerBlock:^(PDGestureTableView *gestureTableView, NSIndexPath *indexPath) {
								NSManagedObject *object = [weakSelf.fetchedResultsController objectAtIndexPath:indexPath];

								[weakSelf.fetchedResultsController deleteObject:object];
							}];

	return cell;
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
	switch(type) {
		[...]

        case NSFetchedResultsChangeDelete:
            [self.gestureTableView deleteCellForIndexPath:indexPath animation:UITableViewRowAnimationRight completion:nil];
            break;

		[...]
    }
}
pushAndDeleteCellForIndexPath:completion:

This method is composed by the two above. You call it when you just want to delete a cell from a table view not being managed by a NSFetchedResultsController. Before calling it you must remove any pertinent data from the data source.

[...]

didTriggerBlock:^(PDGestureTableView *gestureTableView, NSIndexPath *indexPath) {
	[dataArray removeObjectAtIndex:indexPath.row];

	[gestureTableView beginUpdates];

    [gestureTableView pushAndDeleteCellForIndexPath:indexPath completion:^{
        // Cell deleted.
    }];

	[gestureTableView endUpdates];
}];

Notice you must use beginUpdates and endUpdates methods before and after calling this method, respectively.

replaceCellForIndexPath:completion:

Replaces the cell to its original position.

[...]

didTriggerBlock:^(PDGestureTableView *gestureTableView, NSIndexPath *indexPath) {
    [gestureTableView replaceCellForIndexPath:indexPath completion:nil];
}];

If you don't want the cell to bounce when replacing, set cellBouncesWhenReplacing to NO.

Also, if you want to set the duration of all these animations, you can set animationsDuration to the amount of time you want.

Wish List

  • Autoscroll when the current moving cell is near the edge of the table view.
  • Cell bouncing when reaching the last action.

Requirements

  • iOS 7 or higher.
  • Automatic Reference Counting (ARC).

License

PDGestureTableView is available under the MIT license.

Also, I'd really love to know you're using it in any of your projects, so send me an email or a tweet and make my day :)

ios-pdgesturetableview's People

Contributors

dlackty 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.