0

我发现了如何在编辑模式下防止 contentView缩进。我的问题是:

如何防止 contentView 而不是 textLabel 的缩进?我已经尝试在 observeValueForKeyPath 中更改 textLabel 的框架,但运气不好,textLabel 的框架始终相同。

我的目标是,当进入编辑模式时,在 textLabel 上有漂亮的小缩进动画,但在 contentView 上没有

谢谢

4

1 回答 1

0

感谢A-Live指出解决方案。为了解决我的问题,我不得不使用基于这个链接和这个链接的解决方案。首先,我必须创建一个 UITableViewCell 的子类,在其中我重新实现了 setEditing 并添加了 observeValueForKeyPath 函数。

第一步,observeValueForKeyPath禁止contentView的缩进,其次setEditing负责textLabel的动画

这是我的代码:

    #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
于 2012-09-20T14:17:09.313 回答