1
votes

I've researched this across but dont seem to have found a solution yet. I have a custom UITableViewCell (with various subviews, including a radio button, labels, etc.). I would like the + and - insert / delete editing controls to appear at the left-most part of the cell when the table view is set to editing.

If I used the standard UITableViewCell, this works perfectly. However, whilst using the custom cell, the controls just dont appear. Anyone has any ideas on how to fix the same please?

Below are some snapshots of my table view code....

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isEditing) {
        if ([tableView isEqual:self.tableView]) {
            if (editingStyle == UITableViewCellEditingStyleInsert) {
                // ...
            }                  
            else if (editingStyle == UITableViewCellEditingStyleDelete) {
                // ...
            }
        }  
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tableView]) {
        if (indexPath.row == 0) {
            return UITableViewCellEditingStyleInsert;
        }
        else {
            return UITableViewCellEditingStyleDelete;
        }
    }
    else {
        return UITableViewCellEditingStyleNone;
    }
}

And the custom table view cell code...

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self configureConstraints];
}

- (void)configureConstraints
{
    // This is where the cell subviews are laid out.
}
2

2 Answers

3
votes

You didn't implement the setEditing:animated: method correctly in your custom cell. You forgot to call super:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    [self setNeedsLayout];
}

It's a rare overridden method that you don't call super.

Unrelated - in your table view code, don't use isEqual: to compare the two table views, use ==.

if (tableView == self.tableView) {

You actually do want to see if they are the same pointers.

0
votes

Source: Custom edit view in UITableViewCell while swipe left. Objective-C or Swift

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Clona" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        //insert your editAction here
    }];
    editAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        //insert your deleteAction here
    }];
    deleteAction.backgroundColor = [UIColor redColor];
    return @[deleteAction,editAction];
}