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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值