单击按钮时单元格如何滑动 [英] How cell swipe on clicking a button
问题描述
我想点击按钮滑动单元格。我在刷一个细胞方面取得了成功。但我想按下单元格中的按钮。我的代码是
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];
}
推荐答案
我认为你的班级 SimpleCell
应包含UIButton,以便正确执行重用。
I think your class SimpleCell
should contain the UIButton, in order perform reusing correctly.
如果您创建一个自定义 UITableViewCell
,它包含单元格上的所有UI操作并运行它们会更简单在单元格内,而不是 UITableView
本身。
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.
让我们看一个例子:
这是customCell.h文件:
Here's the customCell.h file:
@interface customCell : UITableViewCell {
UIButton *buttonSwipe;
}
@end
这是customCell.h文件:
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.
但如果你想要更快的方式,我建议你去访问上的 Chris Wendel 和他的 SWTableViewCell
GitHub
But if you want a faster way, I recommend you to visit Chris Wendel and his SWTableViewCell
on GitHub
这篇关于单击按钮时单元格如何滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!