vikram17000 vikram17000 - 2 months ago 17
iOS Question

Insert / Delete editing controls not appearing while editing custom UITableViewCell

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

Answer

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.