完成的效果如下
1、添加左滑按钮(多个)
添加左滑按钮 ,这个网上比较多,直接上代码!
//设置是否可以左滑显示按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//这里的title需要设置为空格
UITableViewRowAction *delBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//做一些处理
}];
UITableViewRowAction *topBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//做一些处理
}];
//修改默认的背景,这个修改的是UITableViewRowActionButton -> View层级的背景色,不是上层Button的背景色(如下图)
delBtn.backgroundColor = JX_LightColorWhite;
topBtn.backgroundColor = JX_LightColorWhite;
return @[delBtn, topBtn];
}
这样就可以显示按钮了。但是如果这样子,只会显示文字,并没有设置图片的地方;所以请看下面↓
2、重新编辑UITableViewRowAction的子视图
重写- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
因为每次左滑cell的时候这个代理都会被调用,所以我们在这里编辑子视图,如下:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
JXMsgAndUserObject *p=[_array objectAtIndex:indexPath.row];
if (@available(iOS 13.0, *)) {
for (UIView * subView in self.tableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >=1) {
UIView *swipeView = subView.subviews.firstObject;
swipeView.backgroundColor = [UIColor clearColor];
if (swipeView.subviews.count >=2) {
UIButton *deleteButton = swipeView.subviews[1];
UIButton *upButton = swipeView.subviews[0];
[self configDeleteButton:deleteButton];
[self configUpButton:upButton cellModel:p];
}
}
}
}
else if (@available(ios 11.0, *)) {
for (UIView * subView in self.tableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewWrapperView")]) {
for (UIView *actionView in subView.subviews) {
if ([actionView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
actionView.backgroundColor = [UIColor clearColor];
if (actionView.subviews.count >=2) {
UIButton *deleteButton = actionView.subviews[1];
UIButton *upButton = actionView.subviews[0];
[self configDeleteButton:deleteButton];
[self configUpButton:upButton cellModel:p];
}
}
}
}
}
}
else{
JXCell *tableCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
for (UIView *subview in tableCell.subviews){
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2){
UIButton *deleteButton = subview.subviews[0];
UIButton *upButton = subview.subviews[1];
[self configDeleteButton:deleteButton];
[self configUpButton:upButton cellModel:p];
}
}
}
}
- (void)configDeleteButton:(UIButton *)deleteButton{
[deleteButton setBackgroundImage:[UIImage createImageWithColor:JX_LightColorF9F9F9] forState:UIControlStateNormal];
CGRect cRect = deleteButton.frame;
cRect.origin.x = deleteButton.frame.origin.x + 2;
cRect.size.width = deleteButton.frame.size.width - 2;
[deleteButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(cRect.size.width);
make.left.mas_equalTo(cRect.origin.x);
}];
UIButton *cellDelBtn = [UIFactory buttonWithImage:@"chat_msg_btn_delete" target:nil selector:nil];
cellDelBtn.frame = CGRectMake(0, 0, 70, 68);
[cellDelBtn setTitle:Localized(@"JX_Delete") forState:UIControlStateNormal];
cellDelBtn.titleLabel.font = SYSFONT(12);
[cellDelBtn setTitleColor:JX_LightTextColor878A8C forState:UIControlStateNormal];
[cellDelBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:0];//此方法是改变button的image、title布局的,在此不放出代码,自行另找
[deleteButton addSubview:cellDelBtn];
[cellDelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(deleteButton);
make.size.mas_equalTo(cRect.size);
}];
}
- (void)configUpButton:(UIButton*)upButton cellModel:(JXMsgAndUserObject *)p{
NSString *str;
if (p.user.topTime) {
str = Localized(@"JX_CancelTop");
}else {
str = Localized(@"JX_Top");
}
[upButton setBackgroundImage:[UIImage createImageWithColor:JX_LightColorF9F9F9] forState:UIControlStateNormal];
CGRect cRect = upButton.frame;
cRect.origin.x = upButton.frame.origin.x + 2;
cRect.size.width = upButton.frame.size.width - 2;
[upButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(cRect.size.width);
make.left.mas_equalTo(cRect.origin.x);
}];
UIButton *cellUpBtn = [UIFactory buttonWithImage:@"chat_msg_btn_upTop" target:nil selector:nil];
cellUpBtn.frame = CGRectMake(0, 0, 70, 68);
cellUpBtn.titleLabel.font = SYSFONT(12);
[cellUpBtn setTitleColor:JX_LightTextColor878A8C forState:UIControlStateNormal];
[cellUpBtn setTitle:str forState:UIControlStateNormal];
[cellUpBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:0];//此方法是改变button的image、title布局的,在此不放出代码,自行另找
[upButton addSubview:cellUpBtn];
[cellUpBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(upButton);
make.size.mas_equalTo(cRect.size);
}];
}
大概说一下以上代码的作用吧:通过遍历子视图找到需要更改的UIButton(实际上是其子类),然后创建一个临时Button,按照自己需要的样式去设置,再添加到获取到的button上,最后添加约束即可。
特别说明:由于ios11,ios13都对图层结构进行了更改,所以针对不同的系统,获取方式不一样