iOS tableViewCell侧滑改变收藏状态

/**
* 图片素材 链接: 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 = ; i < ; i++) {
if (!staues) {
staues = [[NSMutableArray alloc] init];
}
[staues addObject:@];//0代表未收藏,1代表已收藏
} } #pragma mark -- UITableViewDataSource --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
} - (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 = ;
@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 < ) {
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-)) && (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), , 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), , 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 = ;
}else{
_statueView.image = [UIImage imageNamed:@"isCollect"];
self.isCollected = ;
}
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
iOS tableViewCell侧滑改变收藏状态的更多相关文章
- iOS开发网络篇—监测网络状态(转)
文章转载自:http://www.cnblogs.com/wendingding/p/3950114.html iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时 ...
- iOS开发网络篇—监测网络状态
iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行 ...
- devexpress中gridview控件编辑时改变输入法状态
在win7环境下使用Devexpress中的SpinEdit控件,切换成中文[简/繁]输入法输入数字键时有不少输入法会重复产生数字如输入1会变成11,输入123会变成112233.使用SpinEdit ...
- iOS 开发网络篇—监测网络状态
iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行 ...
- Android之怎样改变焦点状态【EditText】
以EditText为例 1.改变焦点状态 password.setOnFocusChangeListener(new OnFocusChangeListener() { @Override publi ...
- 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)
排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并不具备任何官方效力, 仅供参考学习, 方便初学者 ...
- WPF 后台数据触发改变界面状态-心跳实现
今年做的一个上位机工控WPF项目,做个小小的总结把,以后随时来找 请不要带血乱喷,我只是菜鸟.___by 鲍队 类似于这样子的;大致的意思是:一个代码变量,通过改变变量的值,绑定这个变量的这个圆颜色也 ...
- iOS - 在工程中试玩状态模式
做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...
- IOS中设置状态栏的状态
IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...
随机推荐
- [ZZ] GTX760首测
再一次让AMD难做!NVIDIA新主力GTX760首测 1又见短板高端显卡,GTX760外观对比回顶部 [PConline评测]NVIDIA迅速的步伐真让人吃惊,短时间内拿出GTX780.GTX770 ...
- QuickStart OpenvirteX
参考:ubuntu14.04安装OpenVirteX 预准备: Java 7 sudo add-apt-repository ppa:webupd8team/java sudo apt-get upd ...
- OAuth2.0协议
简介 OAuth(Open Authorization),协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAuth的授权不会使第三方触及到用户的帐号信息(如用户 ...
- cookie 操作
//创建并赋值 重新赋值也是这样操作 document.cookie="userId=828"; document.cookie="userName=hulk" ...
- Comparable与Comparator
转载 Comparable与Comparator的区别 (转载) Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部 ...
- ios 消息传递机制
引用文章 一.KVO 1.当对象中的某个属性值发生了改变,可以对这些值的观察者做出通知. 2.接受者(会接收到值发生改变的消息) 必须知道发送者(值将发生改变的那个对象). 3.接收者同样还需要知道发 ...
- C# DateTime类型和时间戳 互相转换
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- yii2 实现多表联查
- MongoDB直接执行js脚本
有时候很大一段命令要执行,中间有错就得重新写,这样超麻烦的,不妨存放于js文件里,然后通过shell命令执行,重复利用率高,修改方便. 比如创建test.js print('=========WECO ...
- struts.xml 配置详解
struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件. 一下分别介绍一下几个struts.xml中常用到的标签 1.<include> 利用includ ...