#pragma mark - UITableViewDelegate
/**
* 只要实现这个方法,就拥有左滑删除功能
* 点击左滑出现的Delete按钮 会调用这个
*/
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
// NSLog(@"commitEditingStyle--");
// [self.wineArray removeObjectAtIndex:indexPath.row];
// [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//}
/**
* 修改默认Delete按钮的文字
*/
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// return @"删除";
//}
//实现这个方法不仅可以实现左滑功能,还可以自定义左滑的按钮,并且实现按钮点击处理的事件
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// self.tableView.editing = YES;
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// [self.tableView reloadData];
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
// 退出编辑模式
self.tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.wineArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return @[action1,action];
}
效果图如下:
#pragma mark - 按钮的点击
- (IBAction)remove {
// 进入编辑模式
// self.tableView.editing = !self.tableView.isEditing;
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
ViewController.m代码如下
- (void)viewDidLoad {
[super viewDidLoad];
// self.tableView.allowsMultipleSelection = YES;
// 告诉tableView在编辑模式下可以多选
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.deletedButton.hidden = YES;
}
//批量删除按钮点击
#pragma mark - 按钮的点击
- (IBAction)MultipleRemove {
// 进入编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
self.deletedButton.hidden = !self.tableView.isEditing;
}
//删除按钮点击
- (IBAction)remove {
// 千万不要一边遍历一边删除,因为每删除一个元素,其他元素的索引可能会发生变化
NSMutableArray *deletedWine = [NSMutableArray array];
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
[deletedWine addObject:self.wineArray[indexPath.row]];
}
// 修改模型
[self.wineArray removeObjectsInArray:deletedWine];
// 刷新表格
// [self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
效果图如下:
// XMGWineCell.h
#import
@class XMGWine;
@interface XMGWineCell : UITableViewCell
/** 模型属性 */
@property (nonatomic, strong) XMGWine *wine;
@end
// XMGWineCell.m
#import "XMGWineCell.h"
#import "XMGWine.h"
@interface XMGWineCell ()
/** 打钩控件 */
@property (nonatomic, weak) UIImageView *checkedImageView;
@end
@implementation XMGWineCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 添加打钩控件
UIImageView *checkedImageView = [[UIImageView alloc] init];
checkedImageView.hidden = YES;
checkedImageView.image = [UIImage imageNamed:@"check"];
[self.contentView addSubview:checkedImageView];
self.checkedImageView = checkedImageView;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 设置打钩的位置和尺寸
CGFloat WH = 24;
CGFloat X = self.contentView.frame.size.width - WH - 10;
CGFloat Y = (self.contentView.frame.size.height - WH) * 0.5;
self.checkedImageView.frame = CGRectMake(X, Y, WH, WH);
// 调整textLabel的宽度
CGRect frame = self.textLabel.frame;
frame.size.width = self.contentView.frame.size.width - WH - 20 - self.textLabel.frame.origin.x;
self.textLabel.frame = frame;
}
- (void)setWine:(XMGWine *)wine
{
_wine = wine;
self.textLabel.text = wine.name;
self.imageView.image = [UIImage imageNamed:wine.image];
self.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money];
// 根据模型的checked属性确定打钩控件显示还是隐藏
if (wine.isCheched) {
self.checkedImageView.hidden = NO;
} else {
self.checkedImageView.hidden = YES;
}
}
@end
// XMGWine.h
#import
@interface XMGWine : NSObject
/**
* 图标
*/
@property (nonatomic ,copy)NSString *image;
/**
* 价格
*/
@property (nonatomic ,copy)NSString *money;
/**
* 名字
*/
@property (nonatomic ,copy)NSString *name;
/** 记录打钩控件的状态 */
@property (nonatomic, assign, getter=isCheched) BOOL checked;
@end
// XMGWine.m
#import "XMGWine.h"
@implementation XMGWine
@end
ViewController.m代码如下:
// ViewController.m
#import "ViewController.h"
#import "XMGWineCell.h"
#import "MJExtension.h"
#import "XMGWine.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 酒数据 */
@property (nonatomic, strong) NSMutableArray *wineArray;
/** 记录用户选中行的索引 */
@property (nonatomic, strong) NSMutableArray *seletedIndexPath;
@end
@implementation ViewController
- (NSMutableArray *)seletedIndexPath
{
if (!_seletedIndexPath) {
_seletedIndexPath = [NSMutableArray array];
}
return _seletedIndexPath;
}
- (NSMutableArray *)wineArray
{
if (!_wineArray) {
_wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
}
return _wineArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - 按钮的点击
- (IBAction)remove {
// 获取要删除的酒模型
NSMutableArray *deletedWine = [NSMutableArray array];
for (NSIndexPath *indexPath in self.seletedIndexPath) {
[deletedWine addObject:self.wineArray[indexPath.row]];
}
// 删除模型
[self.wineArray removeObjectsInArray:deletedWine];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:self.seletedIndexPath withRowAnimation:UITableViewRowAnimationAutomatic];
// 清空数组
[self.seletedIndexPath removeAllObjects];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 修改模型
XMGWine *wine = self.wineArray[indexPath.row];
if (wine.isCheched) { // 之前是打钩的,取消打钩
wine.checked = NO;
[self.seletedIndexPath removeObject:indexPath];
} else { // 之前不是打钩的,现在打钩
wine.checked = YES;
[self.seletedIndexPath addObject:indexPath];
}
// 刷新表格
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.wineArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"wine";
XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[XMGWineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.wine = self.wineArray[indexPath.row];
return cell;
}
@end
效果图如下: