iOS cell 左滑删除修改样式

1、 controller.m

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    //刷新cell布局,解决有时候图片无法显示出来的问题
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.isEdit =self.isEdit;
    [cell setNeedsLayout];
    UITableViewRowAction *delegateAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
       ...
    }];
    return @[delegateAction];
}

2、tableViewCell.m

//左划出现分类与删除图标
- (void)layoutSubviews {
    [super layoutSubviews];
    [self setDealDeleteButtonAndClassButton];
}
- (void)setDealDeleteButtonAndClassButton{
    
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue>=11) {
        // iOS 11层级 (Xcode 9编译): UITableView -> UISwipeActionPullView
        for (UIView *subview in self.superview.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])  {
                // 和iOS 10的按钮顺序相反
                UIButton *deleteButton = subview.subviews[0];
                [deleteButton setImage:[UIImage imageNamed:@"OneTime_finish2"] forState:(UIControlStateNormal)];
                [deleteButton setTitle:@"" forState:UIControlStateNormal];
            }
        }
    } else {
        // iOS 9-10层级
        for (UIView *subview in self.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
                UIButton *deleteButton = subview.subviews[0];
                [deleteButton setImage:[UIImage imageNamed:@"OneTime_finish2"] forState:UIControlStateNormal];
                [deleteButton setTitle:@"" forState:UIControlStateNormal];
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容