// 主要代码
第一步:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
第二步:
#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
// 1. 更新数据
//[_allDataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
//这个可以用来刷新特定行
// [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
// 删除一个置顶按钮
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了置顶");
// 1. 更新数据
//[_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
// 2. 更新UI
//NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
// 添加一个更多按钮
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 将设置好的按钮放到数组中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
editingStyle = UITableViewCellEditingStyleDelete;
}
第三步:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
editingStyle = UITableViewCellEditingStyleDelete;
}
第四步:(如果需要更改 删除或编辑 按钮的高度 宽度的话,在cell中重写此方法)
// 改变滑动删除按钮样式
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews){
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
CGRect cRect = subView.frame;
cRect.origin.y = self.contentView.frame.origin.y+10;
cRect.size.height = self.contentView.frame.size.height - 10;
subView.frame = cRect;
UIView *confirmView=(UIView *)[subView.subviews firstObject];
// 改背景颜色
confirmView.backgroundColor=[UIColor colorWithRed:254/255.0 green:85/255.0 blue:46/255.0 alpha:1];
for(UIView *sub in confirmView.subviews){
if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){
UILabel *deleteLabel=(UILabel *)sub;
// 改删除按钮的字体
deleteLabel.font=[UIFont boldSystemFontOfSize:15];
// 改删除按钮的文字
deleteLabel.text=@"删除";
}
}
break;
}
}
}