Is there any way to hide "-" (Delete) button while editing UITableView

On my iphone app, I have a UITableView in edit mode, where user is allowed only to reorder the rows no delete permission is given.

So is there any way where I can hide "-" red button from TableView. Please let me know.

Thanks


Solution 1:

Here is my complete solution, without indentation (0left align) of the cell!

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone; 
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}


- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

Solution 2:

Swift 5 equivalent to accepted answer with just the needed funcs:

extension YourViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .none
    }
}

Solution 3:

This stops indentation:

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

Solution 4:

I faced a similar problem where I wanted custom checkboxes to appear in Edit mode but not the '(-)' delete button.

Stefan's answer steered me in the correct direction.

I created a toggle button and added it as an editingAccessoryView to the Cell and wired it to a method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ....
    // Configure the cell...

    UIButton *checkBoxButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 32.0f)];
    [checkBoxButton setTitle:@"O" forState:UIControlStateNormal];
    [checkBoxButton setTitle:@"√" forState:UIControlStateSelected];
    [checkBoxButton addTarget:self action:@selector(checkBoxButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
    cell.editingAccessoryView = checkBoxButton;

    return cell;
}

- (void)checkBoxButtonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
}

Implemented these delegate methods

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}