/**
* 图片素材 链接: 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侧滑改变收藏状态的更多相关文章

  1. iOS开发网络篇—监测网络状态(转)

    文章转载自:http://www.cnblogs.com/wendingding/p/3950114.html iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时 ...

  2. iOS开发网络篇—监测网络状态

    iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行 ...

  3. devexpress中gridview控件编辑时改变输入法状态

    在win7环境下使用Devexpress中的SpinEdit控件,切换成中文[简/繁]输入法输入数字键时有不少输入法会重复产生数字如输入1会变成11,输入123会变成112233.使用SpinEdit ...

  4. iOS 开发网络篇—监测网络状态

    iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行 ...

  5. Android之怎样改变焦点状态【EditText】

    以EditText为例 1.改变焦点状态 password.setOnFocusChangeListener(new OnFocusChangeListener() { @Override publi ...

  6. 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)

    排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并不具备任何官方效力, 仅供参考学习, 方便初学者 ...

  7. WPF 后台数据触发改变界面状态-心跳实现

    今年做的一个上位机工控WPF项目,做个小小的总结把,以后随时来找 请不要带血乱喷,我只是菜鸟.___by 鲍队 类似于这样子的;大致的意思是:一个代码变量,通过改变变量的值,绑定这个变量的这个圆颜色也 ...

  8. iOS - 在工程中试玩状态模式

    做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...

  9. IOS中设置状态栏的状态

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...

随机推荐

  1. ASP.NET MVC系列 框架搭建(二)之仓储层的优化

    大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...

  2. Nginx 笔记与总结(8)Location:归纳总结

    首先对 URI 进行精准匹配,如果匹配上则退出匹配,返回精准匹配结果: 如果没有匹配上则寻找普通匹配,如果命中多个普通匹配,则记忆最长的匹配结果(不考虑 location 的顺序): 如果后面还有正则 ...

  3. Web 在线文件管理器学习笔记与总结(17)复制文件 (18)剪切文件

    (17)复制文件 ① 复制文件通过copy($src,$dst) 来实现 ② 检测目标目录是否存在,如果存在则继续检测目标目录中是否存在同名文件,如果不存在则复制成功 file.func.php 中添 ...

  4. mysql null值的特殊处理

    实例 尝试以下实例: root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Databa ...

  5. IDM 通过防火墙规则阻止激活验证

    1. 打开Windows防火墙 2. 高级设置-->出站规则-->新建规则 3. 添加IDM程序路径,阻止连接 4. 在属性中添加作用域,远程IP地址: DNS解析出IP:register ...

  6. [转]通过Mesos、Docker和Go,使用300行代码创建一个分布式系统

    http://www.csdn.net/article/2015-07-31/2825348 [编者按]时下,对于大部分IT玩家来说,Docker和Mesos都是熟悉和陌生的:熟悉在于这两个词无疑已成 ...

  7. love yy-kiss 医生出差二期

    医生出差二期,做了一半撤出去做互联网医院了 http://confluence.haodf.net/pages/viewpage.action?pageId=15801359  医生出差二期 没设置数 ...

  8. Delphi 记录类型- 结构指针

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. opencv提取截获图像(总结摘来)

    opencv提取截获图像(总结摘来) http://blog.csdn.net/wuxiaoyao12/article/details/7305865 版权声明:本文为博主原创文章,未经博主允许不得转 ...

  10. Linq中常用的方法

    这几天闲着也是闲着,就仔细的研究了一下Linq的语法,还有他的一些扩展方法的使用. 下面是一些常用的扩展方法. Aggregate 自定义的聚合计算 All 检测序列中所有元素是否都满足指定的条件 A ...