代码改变世界

iOS tableViewCell侧滑改变收藏状态

2016-02-18 15:30  甘雨路  阅读(546)  评论(0编辑  收藏  举报

                     

/**
 *  图片素材   链接: http://pan.baidu.com/s/1mhi1sfQ 密码: w2wq
 */
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
    self.window.rootViewController = navi;
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "LFCustomCell.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,LFCustomCellDelegate>
{
    UITableView *_tableView;
    NSMutableArray *staues;
}
@end

@implementation RootViewController

- (void)loadView{
    [super loadView];
    _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"侧滑收藏";
    
    for (int i = 0; i < 15; i++) {
        if (!staues) {
            staues = [[NSMutableArray alloc] init];
        }
        [staues addObject:@0];//0代表未收藏,1代表已收藏
    }
    
}

#pragma mark -- UITableViewDataSource --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"customCell";
    LFCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LFCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.delegate = self;
    }
    cell.contentLabel.text = [NSString stringWithFormat:@"测试的cell 编号:%ld",indexPath.row];
    cell.isCollected = [staues[indexPath.row] intValue];
    cell.indexPath = indexPath;
    return cell;
}
#pragma mark -- LFCustomCellDelegate --
- (void)processDataWithIndexPath:(NSIndexPath *)indexPath andStatue:(int)statue{
    NSLog(@"indexPath:%@ , statue:%d",indexPath,statue);
    [staues replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:statue]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
#import <UIKit/UIKit.h>

@protocol LFCustomCellDelegate <NSObject>

- (void)processDataWithIndexPath:(NSIndexPath*)indexPath andStatue:(int)statue;

@end

@interface LFCustomCell : UITableViewCell

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

@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIImageView *statueView;
@property(nonatomic, assign) int isCollected;
@property(nonatomic, strong) NSIndexPath *indexPath;

@end
#import "LFCustomCell.h"
#define CELL_WIDTH [UIScreen mainScreen].bounds.size.width
const float animateDuration = 0.3;
const double statueViewWidth = 100;
@interface LFCustomCell ()<UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL isLeft;

@end
@implementation LFCustomCell
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor orangeColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.isLeft = YES;
        [self addSubview:self.contentLabel];
        [self addSubview:self.statueView];
    }
    return self;
}
/**
 *  初始化_contentLabel
 */
- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.backgroundColor = [UIColor whiteColor];
        _contentLabel.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        pan.delegate = self;
        [_contentLabel addGestureRecognizer:pan];
    }
    return _contentLabel;
}
/**
 *  初始化_statueView
 */
- (UIImageView *)statueView{
    if (!_statueView) {
        _statueView = [[UIImageView alloc] init];
    }
    return  _statueView;
}

- (void)panGestureAction:(UIPanGestureRecognizer*)sender{
    CGPoint translation = [sender translationInView:self];

    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            break;
        case UIGestureRecognizerStateChanged:
            //当垂直拖拽时,不执行方法
            if (fabs(translation.x)<fabs(translation.y)) {
                return;
            }
            //向左滑动时,移动_contentLabel;向右滑动时,直接返回
            if (translation.x < 0 ) {
                if (fabs(translation.x)<CELL_WIDTH/2.0) {
                    CGRect frame = _contentLabel.frame;
                    frame.origin.x = translation.x;
                    _contentLabel.frame = frame;
                    CGRect otherFrame = _statueView.frame;
                    otherFrame.origin.x = CGRectGetMaxX(_contentLabel.frame);
                    _statueView.frame = otherFrame;
                }
                if ((fabs(translation.x) > (CELL_WIDTH/2.0-50)) && (self.isLeft == YES)) {
                    [self changeCollectionStatue];
                    self.isLeft = NO;
                }
            }else{
                return;
            }
            break;
        default:
            [UILabel animateWithDuration:animateDuration animations:^{
                _contentLabel.frame = self.bounds;
                _statueView.frame = CGRectMake(CGRectGetMaxX(self.contentLabel.frame), 0, statueViewWidth, self.bounds.size.height);
            } completion:^(BOOL finished) {
                self.isLeft = YES;
            }];
            break;
    }
}

- (void)layoutSubviews{
    self.contentLabel.frame = self.bounds;
    self.statueView.frame = CGRectMake(CGRectGetMaxX(self.contentLabel.frame), 0, statueViewWidth, self.bounds.size.height);
    if (self.isCollected) {
        _statueView.image = [UIImage imageNamed:@"isCollect"];
    }else{
        _statueView.image = [UIImage imageNamed:@"noCollected"];
    }
}

- (void)changeCollectionStatue{
    if (self.isCollected) {
        _statueView.image = [UIImage imageNamed:@"cancleCollect"];
        self.isCollected = 0;
    }else{
        _statueView.image = [UIImage imageNamed:@"isCollect"];
        self.isCollected = 1;
    }
    if ([_delegate respondsToSelector:@selector(processDataWithIndexPath:andStatue:)]) {
        [_delegate processDataWithIndexPath:self.indexPath andStatue:self.isCollected];
    }
}
#pragma mark -- UIGestureRecognizerDelegate --
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

@end