iOS 自定义cell侧滑删除、编辑等按钮

来源:互联网 发布:java上传文件到本地 编辑:程序博客网 时间:2024/06/03 09:50

iOS自定义cell侧滑删除、编辑等按钮,用的是一个网上大神封装好的类,直接引用,就ok了,简单粗暴,适配支持iOS9.2、Xcode7.2
要下载源码的请猛戳这里下载

下面是效果图这里写图片描述

ViewController.m中得方法

////  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的代码

////  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
0 0
原创粉丝点击