iOS自定义cell代码

释放双眼,带上耳机,听听看~!
获取当前左滑cell的index
@interface InvoiceListTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSIndexPath* editingIndexPath;  //当前左滑cell的index,
@end
左滑代理
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"哈哈哈哈");
        [tableView setEditing:NO animated:YES];
        
    }];
    return @[action];
    
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    self.editingIndexPath = indexPath;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setupSlideBtn];
    });
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.editingIndexPath = nil;
}
自定义按钮
// 设置左滑菜单按钮的样式
- (void)setupSlideBtn
{
    // 判断系统是否是iOS11及以上版本
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.subviews) {
            
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1)
            {
                // 修改备注
                UIView *remarkContentView = subView.subviews[0];
                [self setupRowActionView:remarkContentView imageName:@"二维码分享"];
            }
            
        }
        
    } else {
        // iOS11以下做法
        InvoiceListTableViewCell * cell = [self cellForRowAtIndexPath:self.editingIndexPath];
        for (UIView *subView in cell.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1)
            {
            
            // 删除
            UIView *deleteContentView = subView.subviews[0];
            [self setupRowActionView:deleteContentView imageName:@"二维码分享"];
            }
        }
    }
}

// 设置背景图片
- (void)setupRowActionView:(UIView *)rowActionView imageName:(NSString *)imageName
{
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [rowActionView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(rowActionView);
        make.width.mas_equalTo(80);
    }];
}

文章参考另一篇

人已赞赏
iOS文章

iOS UIImagePNGRepresentation崩溃

2019-11-4 10:02:13

iOS文章

iOS 崩溃信息收集实践

2019-11-4 12:07:41

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索