我找到了如何在编辑模式there中阻止contentView的缩进。我的问题是:
如何防止contentView缩进而不是textLabel?我已经尝试在observeValueForKeyPath中更改textLabel的框架,但运气不好,textLabel的框架始终是相同的。
我的目标是,在进入编辑模式时,在textLabel上使用漂亮的小缩进动画,但不在contentView上
谢谢
答案 0 :(得分:0)
感谢A-Live指出解决方案。要解决我的问题,我必须使用基于此link和此link的解决方案。首先,我必须创建一个UITableViewCell的子类,在那里我重新实现了setEditing并添加了observeValueForKeyPath函数。
在第一步,observeValueForKeyPath禁止内容缩进,其次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