一般的情況下我們都會使用系統自帶的側滑刪除,但是有局限性,只有文字沒有圖片.如果碰上產品或者ui沒得商量的脾氣.只好忍氣吞聲硬著頭皮來了.
想法是在cell中添加一個可以滑動的控件
目前來看能滑動的控件Tableview ScrollView Collectionview.就用scrollview來作為滑動控件
設置scrollview的滾動范圍就是 當前cell的寬度加上刪除按鈕的寬度
我們只需要將將要顯示的數據添加到scrollview上即可
cell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@protocol DeleteTableViewCellDelegate <NSObject>
- (void)cellScrollDelete:(UITableViewCell *)cell;
@end
@interface DeleteTableViewCell : UITableViewCell
@property UILabel *nameLabel;
@property id<DeleteTableViewCellDelegate> delegate;
@property UIScrollView *mainScrollView;
@property UIButton *deleteBtn;
@end
NS_ASSUME_NONNULL_END
cell.m
#import "DeleteTableViewCell.h"
@interface DeleteTableViewCell()<UIScrollViewDelegate>
@property UIView *bgView;
@end
@implementation DeleteTableViewCell
- (void)awakeFromNib {
[super 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.contentView.layer.masksToBounds = YES;
self.mainScrollView = [[UIScrollView alloc] init];
self.mainScrollView.showsHorizontalScrollIndicator = NO;
self.mainScrollView.delegate = self;
self.mainScrollView.userInteractionEnabled = YES;
[self.contentView addSubview:self.mainScrollView];
// 這就可以隨意更改刪除樣式了
self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];
[self.deleteBtn setBackgroundColor:[UIColor redColor]];
[self.mainScrollView addSubview:self.deleteBtn];
self.bgView = [[UIView alloc] init];
self.bgView.backgroundColor = [UIColor whiteColor];
self.bgView.layer.cornerRadius = 10;
self.bgView.layer.masksToBounds = YES;
[self.mainScrollView addSubview:self.bgView];
self.nameLabel = [[UILabel alloc] init];
self.nameLabel.font = [UIFont systemFontOfSize:19];
[self.bgView addSubview:self.nameLabel];
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
// 設置滑動視圖的偏移量是:屏幕寬+刪除按鈕寬
self.mainScrollView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
self.mainScrollView.contentSize = CGSizeMake(96 + self.contentView.frame.size.width, 0);
self.deleteBtn.frame = CGRectMake(self.contentView.frame.size.width+16, 0, 80, self.contentView.frame.size.height);
self.bgView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
self.nameLabel.frame = CGRectMake(16, 16, self.bgView.frame.size.width-32-50, 30);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (self.delegate) {
[self.delegate cellScrollDelete:self];
}
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint movePoint = self.mainScrollView.contentOffset;
// NSLog(@"%f",movePoint.x);
if (movePoint.x < 0) {
[self.mainScrollView setContentOffset:CGPointMake(0, 0)];
}
// 拖拽時 改變self.deletebutton 的大小
// 防止我們使勁往左側滑動時deletebtn會緊貼著self.bgview
if (movePoint.x > 96) {
self.deleteBtn.frame = CGRectMake(self.contentView.frame.size.width+movePoint.x-80, 0, 80, self.contentView.frame.size.height);
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
CGPoint endPoint = self.mainScrollView.contentOffset;
if (endPoint.x < 96) {
[self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
調用當前自定義側滑刪除cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DeleteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"applyPurchasePlanReuse"];
if (!cell) {
cell = [[DeleteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"applyPurchasePlanReuse"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
cell.nameLabel.text = [NSString stringWithFormat:@"%ld.數據",indexPath.row + 1];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
// 這里是tableview剛開始滾動時將上一次操作的cell側滑刪除返回到正常狀態
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
DeleteTableViewCell *tempCell = [self.tempTableview cellForRowAtIndexPath:self.openindexPath];
[tempCell.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
// 實現的協議方法
- (void)cellScrollDelete:(UITableViewCell *)cell{
// 1.查找當前的cell的indexpath
NSIndexPath *tempIndexPath = [self.tempTableview indexPathForCell:cell];
// 2.判斷上一次操作的cell是否與本次的一致
if (tempIndexPath.row != self.openindexPath.row) {
// 不一致時,將上一次的側滑刪除返回到正常狀態
DeleteTableViewCell *tempCell = [self.tempTableview cellForRowAtIndexPath:self.openindexPath];
[tempCell.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
// 3.重新賦值
self.openindexPath = tempIndexPath;
}