开发者

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

开发者 https://www.devze.com 2023-01-03 06:49 出处:网络
On my iphone app, I have a UITableView in edit mode, where user is allowed only to reorder the rows no dele开发者_开发百科te permission is given.

On my iphone app, I have a UITableView in edit mode, where user is allowed only to reorder the rows no dele开发者_开发百科te permission is given.

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

Thanks


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


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
    }
}


This stops indentation:

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号