100
votes

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

5

5 Answers

266
votes

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;
}
45
votes

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

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

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}
4
votes

This stops indentation:

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

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;
}
0
votes

When you only want to hide the (-) dot while editing but you may want to keep the deleting functionality for the users you implement it like so in your UITableViewDelegate protocol conforming class

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) return UITableViewCellEditingStyleNone;
    return UITableViewCellEditingStyleDelete;
}