Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell custom edit view

I am trying to replace the default editing mode behaviour of the cells.

I dont want the red circle with line left accessory view to appear.

Upon entering edit mode I need it to shift the content view left & display my delete button.

What I have so far (custom UITableViewCell) overrides:

-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    /* don't call super */
//    [super setEditing:editing animated:animated];
    _isEditing = editing;

    CGFloat newContentViewX = 0.0f;
    UIColor *newDeleteButtonColor = [UIColor clearColor];

    if ( _isEditing )
    {
        newContentViewX = -40.0f;
        newDeleteButtonColor = [UIColor redColor];
    }

    if ( animated )
    {
        [UIView animateWithDuration:0.5f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^
         {
             self.contentView.x = newContentViewX; //change frame.origin.x
             _deleteButton.backgroundColor = newDeleteButtonColor;
         }
                         completion:nil];
    }
    else
    {
        self.contentView.x = newContentViewX; //change frame.origin.x
        _deleteButton.backgroundColor = newDeleteButtonColor;
    }
}

-(BOOL) editing
{
    return _isEditing;
}

& this is the result: setEditing:NOsetEditing:YES

The above code works great! Until it starts scrolling & the contentview gets reset. back to the first image. The editing flag is not persisting my contentview frame changes.

I have added this line:

[cell setEditing:_tableView.isEditing animated:NO];

in cellForRowAtIndexPath & although it is calling & setting edit mode correctly my contentview is still in the original position & not the updated

Short youtube video explaining issue: Youtube link

Anyone else had this problem?

like image 386
Ryan Avatar asked Jul 12 '13 13:07

Ryan


3 Answers

Don't tamper with the content view frame. Just create your button and set it as editingAccessoryView and the presentation will be done for you.

like image 135
Wain Avatar answered Oct 04 '22 09:10

Wain


Your problem is that UITableView is "helpfully" setting up the appropriate state layout with the cell you return from cellForRowAtIndexPath.

Simplest way to deal with this is to override -layoutSubviews in your cell class.

- (void)layoutSubviews
{
   if (self.editing)
  {
     CGRect bounds = self.bounds;
     // do your custom layout
  }
  else
    [super layoutSubviews];
}

That should get things pretty much sorted.

like image 38
Alex Curylo Avatar answered Oct 05 '22 09:10

Alex Curylo


It appears that you are suffering from a side effect of how the UITableView passes the setEditting message to its child cells. The docs state that the TV sends this message to the VISIBLE cells (only). Thus, when a new/dequeued cell becomes visible it has not had the setEditting message, and thus has _editting=NO. Therefore, you need to test and set the cell's _editting=YES in the tableView:cellForRowAtIndexPath: method, then you should get the scrolling behaviour WITH the revealed delete button that you want.

like image 31
Linasses Avatar answered Oct 05 '22 09:10

Linasses