Skip to content

Commit

Permalink
1) 滑动的时候取消所有编辑状态
Browse files Browse the repository at this point in the history
2) 封装了actionButton
3) 优化了初始化方法
  • Loading branch information
SergioChan committed Sep 23, 2015
1 parent 8463f8e commit e1575e2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
4 changes: 3 additions & 1 deletion SCTableView/SCTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ typedef NS_ENUM(NSInteger, SCTableViewCellStyle) {

@property (nonatomic, weak) id<SCTableViewCellDelegate> delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier inTableView:(UITableView *)tableView withSCStyle:(SCTableViewCellStyle)sc_style;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier inTableView:(UITableView *)tableView;

+ (void)endEditing;
@end
12 changes: 10 additions & 2 deletions SCTableView/SCTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ @interface SCTableViewCell()

@implementation SCTableViewCell

/**
* 滑动的时候取消所有cell的编辑状态
*/
+ (void)endEditing
{
[[NSNotificationCenter defaultCenter] postNotificationName:SCNotificationExitEditing object:nil userInfo:nil];
}

- (void)awakeFromNib {
// Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier inTableView:(UITableView *)tableView withSCStyle:(SCTableViewCellStyle)sc_style
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier inTableView:(UITableView *)tableView
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
self.style = sc_style;
self.contentView.backgroundColor = [UIColor whiteColor];
self.touchBeganPointX = 0.0f;
self.dragAnimationDuration = 0.2f;
Expand Down Expand Up @@ -250,6 +257,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if(touch.tapCount > 1)
{
//双击事件可以由其他recognizer捕获到
NSLog(@"double tap!");
[super touchesEnded:touches withEvent:event];
return;
}
Expand Down
1 change: 1 addition & 0 deletions SCTableView/SCTableViewCellRowActionButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

@interface SCTableViewCellRowActionButton : UIButton

- (id)initWithTitle:(NSString *)title color:(UIColor *)color;
@end
17 changes: 11 additions & 6 deletions SCTableView/SCTableViewCellRowActionButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

@implementation SCTableViewCellRowActionButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
- (id)initWithTitle:(NSString *)title color:(UIColor *)color
{
self = [super init];
if(self)
{
self.backgroundColor = color;
[self setTitle:title forState:UIControlStateNormal];
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.contentEdgeInsets = UIEdgeInsetsMake(0,15,0,0);
}
return self;
}
*/

@end
33 changes: 11 additions & 22 deletions SCTableView/SCTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "SCTableViewController.h"
#import "SCTableViewCell.h"
#import "SCTableViewCellRowActionButton.h"

@interface SCTableViewController () <SCTableViewCellDelegate>
@property (nonatomic, strong) NSMutableArray *data;
Expand Down Expand Up @@ -58,7 +59,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
SCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
if(!cell)
{
cell = [[SCTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier" inTableView:self.tableView withSCStyle:SCTableViewCellStyleRight];
cell = [[SCTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier" inTableView:self.tableView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
}
Expand Down Expand Up @@ -89,27 +90,9 @@ - (NSArray *)SCTableView:(UITableView *)tableView leftEditActionsForRowAtIndexPa

- (NSArray *)SCTableView:(UITableView *)tableView rightEditActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *actionButton_1 = [[UIButton alloc]init];
actionButton_1.backgroundColor = [UIColor lightGrayColor];
actionButton_1.tag = 0;
[actionButton_1 setTitle:@"更多" forState:UIControlStateNormal];
actionButton_1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
actionButton_1.contentEdgeInsets = UIEdgeInsetsMake(0,15,0,0);

UIButton *actionButton_2 = [[UIButton alloc]init];
actionButton_2.backgroundColor = [UIColor orangeColor];
actionButton_2.tag = 1;
[actionButton_2 setTitle:@"旗标" forState:UIControlStateNormal];
actionButton_2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
actionButton_2.contentEdgeInsets = UIEdgeInsetsMake(0,15,0,0);

UIButton *actionButton_3 = [[UIButton alloc]init];
actionButton_3.backgroundColor = [UIColor redColor];
actionButton_3.tag = 2;
[actionButton_3 setTitle:@"删除" forState:UIControlStateNormal];
actionButton_3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
actionButton_3.contentEdgeInsets = UIEdgeInsetsMake(0,15,0,0);

SCTableViewCellRowActionButton *actionButton_1 = [[SCTableViewCellRowActionButton alloc]initWithTitle:@"更多" color:[UIColor lightGrayColor]];
SCTableViewCellRowActionButton *actionButton_2 = [[SCTableViewCellRowActionButton alloc]initWithTitle:@"旗标" color:[UIColor orangeColor]];
SCTableViewCellRowActionButton *actionButton_3 = [[SCTableViewCellRowActionButton alloc]initWithTitle:@"删除" color:[UIColor redColor]];
return @[actionButton_1,actionButton_2,actionButton_3];
}

Expand All @@ -128,6 +111,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
{
NSLog(@"did select at %@",indexPath);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.tableView endEditing:YES];
[SCTableViewCell endEditing];
}
/**
* 这是系统的实现方式,无法做到邮箱的效果,但可以做到微信和QQ的左滑效果,iOS8以上才有
*
Expand Down

0 comments on commit e1575e2

Please sign in to comment.