“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
preface
TableView is a very commonly used UI control in the development of the project, the system’s UITableView function has also been very powerful, there is a function, may be we do not race often used, that is the tableView left slide delete function, the system’s left slide delete is also very good-looking, But the business will always have a variety of minimum requirements, so we will have the need to customize the left slide deletion style, so if the custom left slide deletion is the function to say today.
implementation
When the tableView is about to go into edit state
- (void) tableView: (UITableView *) tableView willBeginEditingRowAtIndexPath: (indexPath NSIndexPath *) {/ / in the main thread is needed here dispatch_async(dispatch_get_main_queue(), ^{ [self setupSlideBtnWithEditingIndexPath:indexPath]; }); }Copy the code
TableView implementation of the proxy method, tableView into the editing state, can be left to delete and subsequent operations
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal Handler :^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {// Delete the action after clicking [tableView setEditing:NO animated:YES];}]; action.backgroundColor = hexColor(0xEB1163); return @[action]; }Copy the code
Custom left – swipe delete style.
When you customize the left slide deletion, you need to determine the system version to operate, otherwise there will be a situation of unmoving
//MARK: - set the left slide the button style - (void) setupSlideBtnWithEditingIndexPath (editingIndexPath {NSIndexPath *) if (@ the available (iOS 13.0, *)) { for (UIView *subView in self.tableView.subviews) { if ([subView IsKindOfClass: NSClassFromString (@ "_UITableViewCellSwipeContainerView")] && [subView. Subviews count] > = 1) {/ / modify images UIView *remarkContentView = subView.subviews.firstObject; [self setupRowActionView:remarkContentView]; } } return; If (@available(iOS 11.0, *)) { for (UIView *subView in self.tableView.subviews) { if ([subView IsKindOfClass: NSClassFromString (@ "UISwipeActionPullView")] && [subView. Subviews count] > = 1) {/ / modify pictures UIView *remarkContentView = subView; [self setupRowActionView:remarkContentView]; } } return; } / / the following iOS11 version MTMineCollectionTableViewCell * cell = [self. The tableView cellForRowAtIndexPath: editingIndexPath]; for (UIView *subView in cell.subviews) { if ([subView IsKindOfClass: NSClassFromString (@ "UITableViewCellDeleteConfirmationView")] && [subView. Subviews count] > = 1) {/ / modify images UIView *remarkContentView = subView; [self setupRowActionView:remarkContentView]; }}} - (void) setupRowActionView rowActionView: (UIView *) {/ / cutting round [rowActionView cl_setCornerAllRadiusWithRadiu: 20]; CGRect frame = rowActionView.frame; frame.origin.y += kFitRealValue(7); frame.size.height -= kFitRealValue(13); rowActionView.frame = frame; / / get a button and set the picture UIButton * button = rowActionView. The subviews. FirstObject; [button setImage:kImageName(@"delete_col") forState:UIControlStateNormal]; [button setTitle:@"" forState:UIControlStateNormal]; }Copy the code