0

I found how to prevent indentation of contentView in editing mode there. My question is :

How to prevent indentation of contentView but not textLabel? I already tried to change the frame of textLabel in observeValueForKeyPath, but not luck, the frame for textLabel is always the same.

My goal is, when entering in edit mode, to have the nice little indentation animation on textLabel, but not on contentView

Thank you

Community
  • 1
  • 1
peterphonic
  • 951
  • 1
  • 19
  • 38

1 Answers1

0

Thanks to A-Live to point out the solution. To resolve my problem, I had to use solution based on both this link and this link. First, I had to create a subclass of UITableViewCell, where I re-implemented setEditing and added observeValueForKeyPath function.

At the fist step, observeValueForKeyPath forbid the indentation of contentView, and secondly setEditing take care of the animation for textLabel

Here is my code :

    #import "UI_CategoryTableViewCell.h"

@implementation UI_CategoryTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    [self.contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];
  }
  return self;
}

- (void)dealloc
{
  [self.contentView removeObserver:self forKeyPath:@"frame"];
  [super dealloc];
}
- (void)layoutSubviews
{
  [super layoutSubviews];

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:0.0f];

  for (UIView *subview in self.subviews) {
    //NSLog(@"%@", NSStringFromClass([subview class]));

    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
      CGRect newFrame = subview.frame;
      newFrame.origin.x = 416;
      newFrame.origin.y = -26;
      subview.frame = newFrame;
    }
    else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) {
      CGRect newFrame = subview.frame;
      newFrame.origin.x = 5;
      newFrame.origin.y = -31;
      subview.frame = newFrame;
    }
  }
  [UIView commitAnimations];

  if ([super showingDeleteConfirmation] || [super isEditing]) {
    self.textLabel.frame = CGRectMake(38,-30, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
  }
  else{
    self.textLabel.frame = CGRectMake(10,-30,self.textLabel.frame.size.width, self.textLabel.frame.size.height);
  }
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
  //NSLog(@"observed value for kp %@ changed: %@",keyPath,change);
  if ( [keyPath isEqual:@"frame"] && object == self.contentView )
  {
    CGRect newFrame = self.contentView.frame;
    CGRect oldFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
    //NSLog(@"frame old: %@  new: %@",NSStringFromCGRect(oldFrame),NSStringFromCGRect(newFrame));

    if ( newFrame.origin.x != 0 )
      self.contentView.frame = oldFrame;
  }
}


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

  if(editing) {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.textLabel.frame = CGRectMake(38,-30, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
    [UIView commitAnimations];
  } else {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.textLabel.frame = CGRectMake(10,-30, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
    [UIView commitAnimations];
  }
}

@end
Community
  • 1
  • 1
peterphonic
  • 951
  • 1
  • 19
  • 38