Code Monkey home page Code Monkey logo

jaswipecell's Introduction

JASwipeCell

Build Status CocoaPods

##Overview

An iOS 8 Mail Inspired. A UITableViewCell subclass that displays customizable left or right buttons that are revealed as the user swipes the cell in either direction. The edge-most buttons will pin to the container view and will execute an event similar to how the delete/archive button work in IOS 8 mail.

##Features

  • Supports adding actionable buttons on either side of the cell.
  • You can customize a button's title text and color.
  • Each button will have a block handler that will execute when pressed.
  • The left/right most button will pin to the top container view as the user swipes all the way. This will also execute that button's action.
  • Supports IOS 7+

Example of the buttons revealing as you swipe left:

image

Example of the right most button pinning to the container view:

image

##Usage

###Available via CocoaPods

Add the following to your Podfile.

pod 'JASwipeCell'

Run pod install from your Terminal. This will install the necessary files.

###Set up the cell

First step is to subclass JASwipeCell.h. You have full control of the views rendered on this cell and they must be added as subviews to JASwipeCell's topContainerView. You can use a xib file or do this in code. The example provided is done in code using PureLayout.

###Set up the buttons

Next step is to set up the left/right buttons. I've created a JAActionButton class that has a class method to quickly create a button with a title, background color, and completion handler.

- (NSArray *)leftButtons
{
    __typeof(self) __weak weakSelf = self;
    JAActionButton *button1 = [JAActionButton actionButtonWithTitle:@"Delete" color:[UIColor redColor] handler:^(UIButton *actionButton, JASwipeCell*cell) {
        [cell completePinToTopViewAnimation];
        [weakSelf leftMostButtonSwipeCompleted:cell];
    }];
    
    JAActionButton *button2 = [JAActionButton actionButtonWithTitle:@"Mark as unread" color:kUnreadButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mark As Unread" message:@"Done!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
    
    return @[button1, button2];
}

- (NSArray *)rightButtons
{
    __typeof(self) __weak weakSelf = self;
    JAActionButton *button1 = [JAActionButton actionButtonWithTitle:@"Archive" color:kArchiveButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        [cell completePinToTopViewAnimation];
        [weakSelf rightMostButtonSwipeCompleted:cell];
    }];
    
    JAActionButton *button2 = [JAActionButton actionButtonWithTitle:@"Flag" color:kFlagButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Flag" message:@"Flag pressed!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
    JAActionButton *button3 = [JAActionButton actionButtonWithTitle:@"More" color:kMoreButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"More Options" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Option 1" otherButtonTitles:@"Option 2",nil];
        [sheet showInView:weakSelf.view];
    }];
    
    return @[button1, button2, button3];
}

###Set up the table view.

Now you must create your cell instances using the button creation methods above.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kJATableViewCellReuseIdentifier];
    
    [cell addActionButtons:[self leftButtons] withButtonWidth:kJAButtonWidth withButtonPosition:JAButtonLocationLeft];
    [cell addActionButtons:[self rightButtons] withButtonWidth:kJAButtonWidth withButtonPosition:JAButtonLocationRight];
    
    cell.delegate = self;
    
    [cell configureCellWithTitle:self.tableData[indexPath.row]];
    [cell setNeedsLayout];
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    
    return cell;
}

###Respond to delegate methods.

There are a list of delegate methods available that get called as swipe events occur on the cell.

@protocol JASwipeCellDelegate <NSObject>
- (void)leftMostButtonSwipeCompleted:(JASwipeCell *)cell;
- (void)rightMostButtonSwipeCompleted:(JASwipeCell *)cell;
@optional
- (void)swipingRightForCell:(JASwipeCell *)cell;
- (void)swipingLeftForCell:(JASwipeCell *)cell;
@end

In the project example, swiping a cell all the way to the left will trigger the "Archive" button to execute. This will call the delegate method rightMostButtonSwipeCompleted:, which is responsible for updating the table's data and deleting the row.

- (void)rightMostButtonSwipeCompleted:(JASwipeCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableData removeObjectAtIndex:indexPath.row];
    
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

##Example Project An example project is provided. It requires IOS7 and can be run on device or simulator. This will work for any device size.

##Creator Jose Alvarez

##License JASwipeCell is available under the MIT license. See the LICENSE file for more info.

jaswipecell's People

Contributors

bcylin avatar joseria avatar lavaslider avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jaswipecell's Issues

Auto-Sizing TableViewCells Not Working

I have Auto-Sizing TableViewCells (new iOS 8 feature). Now I incorporated JASwipeCell, and the autosizing is broken. Seems like the topContentView does not behave correctly. I noticed that the topContentView is added to the UITableView itself, but for the autosizing to work, the content must be added to the contentView of the UITableView.

Faulty on iOS 9

Hey there, Jose!
First let me tell you that this is a great implementation of the Swipe Cells in the Mail App. I had been trying to look for this very implementation since quite a long time.
Getting to the issue, the JAActionButtons are visible only on a random cell in the tableview. Swiping left works brilliantly, but while swiping left, the animation of the buttons are fuzzy.
P.S. I am running the same code as the ViewController in my project

Support CocoaPods

It would be great if your control would be available over CocoaPods

using ib not called

I use Storyboard and make connection between ib and code.
This Cause

  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    NOT CALLED.
    Instead I put
  • (instancetype)initWithCoder:(NSCoder *)aDecoder
    to initialize.
    but the content can not be shown.
    Do you have any idea.

landscape / AutoLayout problem

dear joseria

can you help me to use your component in Landscape mode ( your demo project also have problem with landscaoe mode)
also my project in Auto Layout can you help me about this ?

Need a way to change button width.

joseria, thanks for cool project.

Need a way to change button width.
Now button width is static constant. I can't change its from my own class.

Only having buttons on one side of the cell

I'm having trouble only using buttons on the right side.

Right now the user can still swipe right on the cell, and then an exception is thrown, because it's expecting an array of buttons for the left side:

'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

How can I disable buttons for the left side, and only have buttons on the right side?

Support for iOS7?

Hi!

Thank you for your great control! Are you supporting iOS 7?

Bests,
Philip

Functionality with cell.accessoryType set?

If I set an accessory disclosure like:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

The cell's content is shifted to the left by the width of the chevron and the right-most button underneath is visible. I can shift the cell to the right and it becomes hidden, showing the chevron properly, but this goes away when the cell is redrawn or shifted left.

Version 1.0.2

Hi,
i'have seen that a fix has been done to the layout cell when the tableView index is enable.
I'have done the same fix on my project :)
This fix is already merged on your Master branch, can you release a new pod version ?

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.