/**
* 图片素材 链接: 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:Repeater 详情 备用

    页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...

  2. [IT新应用]农民朋友的电子商务

    今天通过http://olympiawa.gov/visitors.aspx olympia市的官网,到 http://www.olympiafarmersmarket.com/vendors-1/到 ...

  3. Ubuntu 14.04 安装 VirtualBox

    参考: ubuntu14.04,安装VirtualBox 5.0(虚拟机软件)! 由于Vagrant工具的需要,安装了一下VirtualBox. 使用参考链接的法一居然在软件中心里面报错,我想可能是没 ...

  4. PHP 对象和引用总结

    PHP 中使用 简单变量 和 对象 时的区别: ① 很多数据类型都可以写时复制(copy-on-write),例: <?php $a = 'test1'; $b = $a; $b = 'test ...

  5. ThinkPHP 3.2.2 视图模板中使用字符串截取函数

    在项目的 Common/function.php 文件里( 项目结构如图 ) 添加函数: /*字符串截断函数+省略号*/ function subtext($text, $length) { if(m ...

  6. [SHELL实例] (转)最牛B的 Linux Shell 命令 (一)

    本文编译自commandlinefu.com ( 应该是 Catonmat ) 的系列文章 Top Ten One-Liners from CommandLineFu Explained .作为一个由 ...

  7. PHP程序员必须清楚的问题汇总

    PHP程序员必须清楚的问题汇总 投稿:hebedich 字体:[增加 减小] 类型:转载   这篇文章主要介绍了PHP程序员必须清楚的问题汇总,需要的朋友可以参考下     你是否正在准备寻找一份PH ...

  8. 20145235《Java程序设计》课程总结

    每周读书笔记链接汇总 20145235<Java程序设计>第1周学习总结 20145235<Java程序设计>第2周学习总结 20145235<Java程序设计>第 ...

  9. JMS相关概念

    1.相关概念 1)JMS jms即Java消息服务(Java Message Service) 是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息 ...

  10. Selenium 遇到的问题

    1. How to Resolve Stale Element Reference Exception? First of all lets be clear about what a WebElem ...