iOS 自定义cell侧滑删除、编辑等按钮
2016-01-09 21:27
465 查看
iOS自定义cell侧滑删除、编辑等按钮,用的是一个网上大神封装好的类,直接引用,就ok了,简单粗暴,适配支持iOS9.2、Xcode7.2
要下载源码的请猛戳这里下载
下面是效果图
![](http://img.blog.csdn.net/20160109212343717)
ViewController.m中得方法
SMMoreOptionsCell.h的代码
要下载源码的请猛戳这里下载
下面是效果图
ViewController.m中得方法
[code]// // ViewController.m // cell侧滑demo // // Created by renjinbo on 15/12/28. // Copyright © 2015年 com.coffee.biggerapple.zxp. All rights reserved. // #import "ViewController.h" #import "SMMoreOptionsCell.h" //使用时候直接将 SMMoreOptionsCell 两个类拖到工程中即可 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,SMMoreOptionsDelegate> @property (nonatomic, strong) UITableView *tabelViewDemo; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.tabelViewDemo = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.tabelViewDemo.delegate = self; self.tabelViewDemo.dataSource = self; [self.view addSubview:self.tabelViewDemo]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; SMMoreOptionsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[SMMoreOptionsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } //代理 cell.delegate = self; cell.scrollViewOptionsWidth = 150; UIView *extraView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 60)]; [cell.scrollViewOptionsView addSubview: extraView]; UILabel *contenLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)]; contenLable.text = @"向右滑动试试会有惊喜"; [cell.scrollViewContentView addSubview:contenLable];//此处文字必须用scrollViewContentView,且用添加子视图的形式添加 //侧滑出现的自定义button extraView.backgroundColor = [UIColor redColor]; UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeSystem]; deleteBtn.frame = CGRectMake(0, 0, 75, 60); deleteBtn.backgroundColor = [UIColor lightGrayColor]; [deleteBtn setImage:[UIImage imageNamed:@"模板编辑_删除"] forState:UIControlStateNormal]; [deleteBtn addTarget:self action:@selector(clickDelete) forControlEvents:UIControlEventTouchUpInside]; [extraView addSubview:deleteBtn]; UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeSystem]; editBtn.frame = CGRectMake(75, 0, 75, 60); editBtn.backgroundColor = [UIColor orangeColor]; [editBtn setImage:[UIImage imageNamed:@"联系人_编辑icon"] forState:UIControlStateNormal]; [extraView addSubview:editBtn]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } -(void)clickDelete { NSLog(@"删除成功"); } #pragma mark - SMMoreOptionsDelegate - (void)didTouchOnDelete:(SMMoreOptionsCell *)cell { } - (void)didTouchOnMore:(SMMoreOptionsCell *)cell { } - (void)cellDidHideOptions:(SMMoreOptionsCell *)cell { } - (void)cellDidShowOptions:(SMMoreOptionsCell *)cell { } @end
SMMoreOptionsCell.h的代码
[code]// // SMMoreOptionsCell.h // SMMoreOptionsCell // // Created by Richard Marktl (@richmarktl) on 23.08.13. // Copyright (c) 2013 Richard Marktl (@richmarktl). All rights reserved. // // #import <UIKit/UIKit.h> @protocol SMMoreOptionsDelegate; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @interface SMMoreOptionsCell : UITableViewCell @property (nonatomic, strong) UIView *scrollViewContentView; // This content view is above the more options view. // This scrollViewOptionsView contains per default the following 2 buttons, feel free to set your own options view. In // the case you set your own custom scrollViewOptionsView, the -didTouchOnDelete: and -didTouchOnMore: are not called. // The options view is always set to hidden if not visible through the scroll behaviour. @property (nonatomic, strong) UIView *scrollViewOptionsView; @property (nonatomic, strong) UIButton *moreButton; // set your own button or modify the existing button @property (nonatomic, strong) UIButton *deleteButton; // set your own button or modify the existing button @property (nonatomic, assign) CGFloat scrollViewOptionsWidth; // set width of the scrollViewOptionsView, default 150 px @property (nonatomic, weak) id<SMMoreOptionsDelegate> delegate; - (void)dismissOptionsAnimated:(BOOL)animated; // in the case the options are visble the view will dismiss them @end //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @protocol SMMoreOptionsDelegate <NSObject> @required - (void)didTouchOnDelete:(SMMoreOptionsCell *)cell; - (void)didTouchOnMore:(SMMoreOptionsCell *)cell; @optional - (void)cellDidHideOptions:(SMMoreOptionsCell *)cell; - (void)cellDidShowOptions:(SMMoreOptionsCell *)cell; @end //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @interface UIImage (SMMoreOptionsCell) + (UIImage *)imageWithColor:(UIColor *)color; @end //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// extern const CGFloat SMMoreOptionsDefaultContentWidth; extern NSString * const SMMoreOptionsShouldHideNotification; // post this notification to hide currently visible options
相关文章推荐
- iOS如何输出BOOL类型值
- iOS启动页的设置
- iOS中#import和@class的区别
- iOS内购错误汇总及解决方案
- iOS navigation (pop)返回按钮点击提示框
- iOS防止消息转发的crash
- IOS开发中常量的处理
- 基于IOS下的支付宝SDK的学习与使用——实现产品支付(一)
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- iOS调用系统相册选取图片
- id和instancetype的区别
- [iOS]技术学习路线图(长期更新)[好久不见,
- [iOS]iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
- iOS开发 X-code技巧
- iOS framework 制作教程
- 详解iOS多线程
- ios多媒体
- Ios系统应用和系统服务
- iOS8 【xcode6中添加pch全局引用文件】
- iOS 进阶 第二十二天(0603)