添加基本方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UIView *subview in tableView.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
if ([NSStringFromClass([subview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {
subview.backgroundColor = [UIColor whiteColor];
UIButton *deleteBtn = subview.subviews[0];
[deleteBtn setWidth:75];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: deleteBtn.bounds byRoundingCorners:
UIRectCornerBottomRight | UIRectCornerTopRightcornerRadii:CGSizeMake(8,8)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = deleteBtn.bounds;
maskLayer.path = maskPath.CGPath;
deleteBtn.layer.mask = maskLayer;
deleteBtn.backgroundColor = HexRGB(0xF3755C);
[deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
}
}
}
}
此方法是ios 11 之后用到的 TARGETS -> Deployment info -> Deployment Target 设置为11.0
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
MJWeakSelf;
UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
if ([NSStringFromClass([sourceView.superview class]) isEqualToString:@"UISwipeActionStandardButton"]) {
UIButton *tmpBtn = (UIButton *)sourceView.superview;
if ([[tmpBtn currentTitle] isEqualToString:@"确认删除"]) {
DeviceRealmModel *model = [weakSelf.tableViewArr objectAtIndex:indexPath.section];
//删除操作
else{
UIImage *img = [self imageWithColor:HexRGB(0xF3755C)];
[tmpBtn setBackgroundImage:img forState:UIControlStateNormal];
[tmpBtn setTitle:@"确认删除" forState:UIControlStateNormal];
}
}
}];
// 此处可以添加多个UIContextualAction 放入下面的configuration 数组中
UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[action]];
configuration.performsFirstActionWithFullSwipe = NO;
return configuration;
}
// 颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
不要问我为什么不用设置背景色来设置颜色,此时 操作完成 主意你的xocde