需求:开发下面这种样式,列表左滑有置顶和删除功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除".ntes_localized handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { [tableView setEditing:NO animated:YES]; completionHandler(YES); }]; UIContextualAction *top = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"置顶" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { NSLog(@"%@",@"置顶"); [tableView setEditing:NO animated:YES]; completionHandler(YES); }]; top.backgroundColor = UIColorFromRGB(0xEEF7FF);
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[delete,top]]; //禁止滑动到底直接执行第一个按钮的事件 config.performsFirstActionWithFullSwipe=NO; return config; } |
修改左滑按钮的样式
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; [CATransaction begin]; [CATransaction setDisableActions:YES]; for (UIView * subView in cell.superview.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) { for (UIView * sonView in subView.subviews) { if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) { UIButton *aBtn = (UIButton *)sonView; aBtn.titleLabel.font = kFont(14); if (![aBtn.currentTitle isEqualToString:@"删除"]) { [aBtn setTitleColor:UIColorFromRGB(0x3072F6) forState:UIControlStateNormal]; } } } } } [CATransaction commit]; } |