您的位置:首页 > 移动开发 > IOS开发

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

2016-01-09 21:27 465 查看
iOS自定义cell侧滑删除、编辑等按钮,用的是一个网上大神封装好的类,直接引用,就ok了,简单粗暴,适配支持iOS9.2、Xcode7.2

要下载源码的请猛戳这里下载

下面是效果图


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: