1

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.
}
vikram17000
  • 453
  • 5
  • 18

2 Answers2

3

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Thanks @rmaddy ... this works perfectly! My oversight in this implementation on the super miss! Thanks much again - I'll just shortly accept the answer after the minimum time! Thanks also for the tip on the equal comparator! – vikram17000 Sep 16 '16 at 16:05
0

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];
}
Community
  • 1
  • 1
user6837640
  • 82
  • 1
  • 9
  • Thanks @user6837640 ... this would indeed show the edit actions but only on a left swipe? I do want the + and - buttons to appear always when the table is set to editing. Essentially like the edit contact view on iOS... or am I missing something please...? – vikram17000 Sep 16 '16 at 16:02