How cell swipe on clicking a button

2020-03-01 09:16发布

I want to swipe cell on click a button . I am succesful on swiping a cell. But i want to swipe on button which is in cell. my code is

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleCell";
    SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    SimpleCell *cekks=[[SimpleCell alloc]init];
    cekks.scrollButton.alpha =0.0;

    NSString *titleString;
    UIButton *sender = [[UIButton alloc]init];
    //[sender setBackgroundImage:[UIImage imageNamed:@"swipe.png"] forState:UIControlStateNormal];
    sender.tag = indexPath.row;

    titleString =@"Send A Gift";
    UITableViewRowAction *sendAGift = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:titleString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
       // [self deleteMail:[NSArray arrayWithObject:indexPath]:YES];

    }];

    [sendAGift setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"swipe.png"]]];



    return @[sendAGift];
}

2条回答
祖国的老花朵
2楼-- · 2020-03-01 10:10

You can call editActionsForRowAtIndexPath: method as

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:SELECTED_ROW_INDEX inSection:0];
    [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];

try the below code

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];


    //swipe button allocation
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            btn.tag = indexPath.row;
            [cell.contentView addSubview:btn];
            [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
            cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
            return cell;
        }
        -(void)buttonTouched:(id)sender{

            UIButton *btn = (UIButton *)sender;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
            [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
        }
        - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
            // Return NO if you do not want the specified item to be editable.
            return YES;
        }

        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
            if (editingStyle == UITableViewCellEditingStyleDelete) {
                [self.objects removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            } else if (editingStyle == UITableViewCellEditingStyleInsert) {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
            }
        }

        -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
            NSLog(@"edit");
            // code
            return nil;
        }
查看更多
家丑人穷心不美
3楼-- · 2020-03-01 10:12

I think your class SimpleCell should contain the UIButton, in order perform reusing correctly.

It will be simpler if you create a custom UITableViewCell that contains all the UI actions over the cell and operate them within the cell, not in the UITableView itself.

Let's see an example:

Here's the customCell.h file:

@interface customCell : UITableViewCell {
    UIButton *buttonSwipe;
}
@end

Here's the customCell.h file:

#import "customCell.h"

@implementation customCell

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (void) commonInit {
    buttonSwipe = [UIButton... // Initialize here your button
    [buttonSwipe addTarget:self action:@selector(swipeCell:) forControlEvents:UIControlEventTouchUpInside]; 
}

- (void) swipeCell {
    // Embed here the code that makes the effect of swipe the cell.
}

This is a fast-untested workaround with some predefined code, but I think it's a working example if you want to make your cells swipe with your own code.

But if you want a faster way, I recommend you to visit Chris Wendel and his SWTableViewCell on GitHub

查看更多
登录 后发表回答