单击按钮时单元格如何滑动

var*_*run 11 objective-c ios8

我想点击按钮滑动单元格.我在刷细胞方面取得了成功.但是我想要按下单元格中的按钮.我的代码是

- (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];
}
Run Code Online (Sandbox Code Playgroud)

Jor*_*mos 3

我认为你的类SimpleCell应该包含 UIButton,以便正确执行重用。

UITableViewCell如果您创建一个包含单元格上所有 UI 操作的自定义项,并在单元格内(而不是单元格本身)操作它们,则会更简单UITableView

让我们看一个例子:

这是 customCell.h 文件:

@interface customCell : UITableViewCell {
    UIButton *buttonSwipe;
}
@end
Run Code Online (Sandbox Code Playgroud)

这是 customCell.h 文件:

#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.
}
Run Code Online (Sandbox Code Playgroud)

这是一个未经测试的快速解决方法,包含一些预定义的代码,但我认为如果您想使用自己的代码使单元格滑动,那么这是一个可行的示例。

但如果你想要更快的方法,我建议你访问Chris Wendel和他SWTableViewCellGitHub