- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@", indexPath);
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"haha";
由于现在越来越多的新需求产生,很多时候我们侧滑会出多个选项.从 iOS 以后,苹果也有了相应的方法:
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"增加" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"%@", indexPath);
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"减少" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"%@", indexPath);
return @[action, action1];
现在下面是自定义实现 多行删除 的部分代码:
@property (nonatomic, strong) NSMutableArray *messageList;
@property (nonatomic, weak) UIButton *editBtn;
@property (nonatomic, strong) NSMutableArray *selectedEditList;
@property (nonatomic, copy) NSString *pushId;
复制代码
- (NSMutableArray *)messageList {
if (!_messageList) {
_messageList = [NSMutableArray array]
return _messageList
- (NSMutableArray *)selectedEditList {
if (!_selectedEditList) {
_selectedEditList = [NSMutableArray array]
return _selectedEditList
复制代码
- (void)viewDidLoad {
[super viewDidLoad]
self.view.backgroundColor = QTXBackgroundColor
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone
// 隐藏无数据的cell
self.tableView.tableFooterView = [[UIView alloc] init]
self.navigationItem.title = @"系统消息"
// 编辑按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]
[btn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside]
[btn setTitle:@"编辑" forState:UIControlStateNormal]
[btn setTitle:@"取消" forState:UIControlStateSelected]
[btn setTitleColor:QTXNavTitleColor forState:UIControlStateNormal]
btn.titleLabel.font = [UIFont systemFontOfSize:14]
// 让按钮内部的所以内容右对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight
btn.size = CGSizeMake(44, 44)
self.editBtn = btn
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:btn]
self.navigationItem.rightBarButtonItem = editItem
复制代码
- (void)editClick:(UIButton *)btn {
btn.selected = !btn.selected
if (btn.selected) {
self.deleteView.hidden = NO
for (QTXSystemMessageModel *model in self.messageList) {
model.edit = YES
model.checked = NO
} else {
self.deleteView.hidden = YES
[self.selectedEditList removeAllObjects]
for (QTXSystemMessageModel *model in self.messageList) {
model.edit = NO
[self.tableView reloadData]
复制代码
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.messageList.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QTXSystemMessageListCell *cell =[QTXSystemMessageListCell cellWithTableView:tableView];
if (self.messageList.count) {
QTXSystemMessageModel *model = self.messageList[indexPath.row];
cell.model = model;
return cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
QTXSystemMessageModel *model = self.messageList[indexPath.row];
return model.cellH;
复制代码
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
QTXSystemMessageModel *model = self.messageList[indexPath.row];
if (self.editBtn.selected) {
model.checked = !model.isChecked;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (model.isChecked) {
[self.selectedEditList addObject:model];
} else {
[self.selectedEditList removeObject:model];
} else {
if ([model.pushStatus isEqualToString:@"1"]) return;
#warning todo :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
return YES;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
QTXSystemMessageModel *model = self.messageList[indexPath.row];
[self uploadDeleteData:model.pushId reloadData:indexPath];
- (void)uploadDeleteData:(NSString *)pushId reloadData:(NSIndexPath *)indexPath {
#warning todo :
if (indexPath) {
[weakSelf.messageList removeObjectAtIndex:indexPath.row]; // 移除当前模型的数据
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; // 移除当前相应的数据源cell
[weakSelf.tableView reloadData];
复制代码
// 在请求消息数据方法里需要加载调用这个方法
// 当前系统消息列表
NSArray *currentPageArray = [QTXSystemMessageModel loadSystemMessageFromJson:json[@"data"]]
[self currentEditStatus:currentPageArray]
// 所有系统消息列表
[weakSelf.messageList addObjectsFromArray:currentPageArray]
- (void)currentEditStatus: (NSArray *)currentPageArray {
if (self.editBtn.selected) {
for (QTXSystemMessageModel *model in currentPageArray) {
model.edit = YES
} else {
for (QTXSystemMessageModel *model in currentPageArray) {
model.edit = NO
// 点击删除事件
- (void)deleteBtnClick {
for (QTXSystemMessageModel *model in self.selectedEditList) {
[self uploadDeleteData:model.pushId reloadData:nil]
[self.messageList removeObjectsInArray:self.selectedEditList]
[self.tableView reloadData]
[self editClick:self.editBtn]
复制代码
效果如下: