Reputation: 877
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:
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?
Upvotes: 13
Views: 8753
Reputation: 395
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.
Upvotes: 1
Reputation: 4770
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.
Upvotes: 2
Reputation: 119041
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.
Upvotes: 3