panning cells with custom action in ios

591 views Asked by At

Hi i am trying to make a tableview with mailbox style panning http://www.mailboxapp.com/ for that i am using this library https://github.com/gloubibou/HHPanningTableViewCell and it is working fine, i swipe the cell and it moves just fine, the problem is that i want to trigger a custom action when i swipe the cell and i have only been able to do it when it is open and then i touch it.

This is the code where the action is happening

    #import "TableViewController.h"

#import "HHPanningTableViewCell.h"


@interface TableViewController ()

@property (nonatomic, retain) NSArray *rowTitles;

@end


@implementation TableViewController


#pragma mark -
#pragma mark Initialization

- (id)init
{

    self = [super initWithNibName:@"TableViewController" bundle:nil];

    if (self != nil) {
        self.rowTitles = [NSArray arrayWithObjects:@"Pan direction: None", @"Pan direction: Right", @"Pan direction: Left", @"Pan direction: Both", @"Custom trigger", nil];
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark -
#pragma mark Accessors

@synthesize rowTitles = _rowTitles;


#pragma mark -
#pragma mark Rotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.rowTitles count] * 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;


    if (cell == nil) {
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:CellIdentifier];

        UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

        // dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
        // Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
        drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_dotted"]];

        cell.drawerView = drawerView;       
    }

    if (directionMask < 3) {
        cell.directionMask = directionMask;
    }
    else {
        cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;

        if (directionMask == 4) {
            cell.delegate = self;
        }
    }

    cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];




    return cell;
}

- (void)gestureRecognizerDidPan:(UIPanGestureRecognizer*)gestureRecognizer{

}

#pragma mark -
#pragma mark Table view delegate



- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     NSInteger directionMask = indexPath.row;
    NSString *celda = [NSString stringWithFormat:@"%d", directionMask];

    [cell isKindOfClass:[HHPanningTableViewCell class]];
    HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;


    if (directionMask == 1) {
        if (HHPanningTableViewCellDirectionRight) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        if ([panningTableViewCell isDrawerRevealed]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"2"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }


    return indexPath;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

#pragma mark -
#pragma mark HHPanningTableViewCellDelegate

- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                    message:@"You triggered a custom action"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

@end

I know i could use a Gesture recognizer to trigger the action but i think the library is already doing that.

in this part i trigger an action knowing exatly the cell, where was it paned to and if the back of the cell is revealed or not, but always by clicking it since it is a select function.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     NSInteger directionMask = indexPath.row;
    NSString *celda = [NSString stringWithFormat:@"%d", directionMask];

    [cell isKindOfClass:[HHPanningTableViewCell class]];
    HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;


    if (directionMask == 1) {
        if (HHPanningTableViewCellDirectionRight) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        if ([panningTableViewCell isDrawerRevealed]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"2"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }


    return indexPath;
} 

and i believe this other part is where the custom action should be trigered but the program never enters this function

- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                    message:@"You triggered a custom action"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

I hope i made myself clear and thank you in advance.

2

There are 2 answers

0
Morekid On

For the delegate method to trigger you need to set your controller as delegate for the cell. Currently in your cellForRowAtIndexPath the controller is assigned as delegate only when directionMask is 4. So you either set directionMask to be 4 in your current code (which is returning a value based on the cell position instead) or you set the controller as delegate in every case, as I've done below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;


    if (cell == nil) {
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:CellIdentifier];

        UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

        // dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
        // Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
        drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_dotted"]];

        cell.drawerView = drawerView;       
    }

    if (directionMask < 3) {
        cell.directionMask = directionMask;
    }
    else {
        cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;

        // previous code
        //if (directionMask == 4) {
        //    cell.delegate = self;
        //}
    }
    cell.delegate = self;

    cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];

    return cell;
}
0
sutee On

Change the delegate method to

- (void)panningTableViewCell:(HHPanningTableViewCell *)cell didTriggerWithDirection:(HHPanningTableViewCellDirection)direction;