将自定义UITableViewCell返回到非编辑状态

时间:2013-03-27 05:46:19

标签: ios uitableview sdk

我已经将UITableViewCell子类化为我的表视图创建自定义布局和动画。基本上,当我向左滑动时,我进入编辑模式,当我向右滑动时,我退出编辑模式 - 每个都有适当的动画。

滑动按预期工作,但是当表视图滚动或其他单元格进入编辑状态时,我似乎无法弄清楚如何将单元格设置为非编辑状态。

每当桌面视图滚动或用户开始编辑(向左滑动)另一个单元格时,想法是将单元格重置为“正常”状态。

这是我的自定义UITableViewCell类:

//
//  CustomTableViewCell.m
//
//  Created by Spencer Müller Diniz on 27/03/13.
//  Copyright (c) 2013 Family. All rights reserved.
//

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

@synthesize customView;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        CGRect customFrame = CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, self.contentView.bounds.size.height);

        self.customView = [[CustomTableViewCellView alloc] initWithFrame:customFrame];
        [self.contentView addSubview:self.customView];

        UISwipeGestureRecognizer* sgrLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
        [sgrLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self addGestureRecognizer:sgrLeft];

        UISwipeGestureRecognizer* sgrRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
        [sgrRight setDirection:UISwipeGestureRecognizerDirectionRight];
        [self addGestureRecognizer:sgrRight];
    }

    return self;
}

- (void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer {
    if (!self.isEditing)
        [self setEditing:YES animated:YES];
}

- (void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer {
    if (self.isEditing)
        [self setEditing:NO animated:YES];
}

- (void)layoutSubviews {
    if (self.isEditing) {
        [UIView animateWithDuration:0.5f animations:^{
            self.customView.forgroundView.frame = CGRectMake(-100.0f, 0.0f, self.customView.forgroundView.frame.size.width, self.customView.forgroundView.frame.size.height);
        }];

    }
    else {
        [UIView animateWithDuration:0.5f animations:^{
            self.customView.forgroundView.frame = CGRectMake(0.0f, 0.0f, self.customView.forgroundView.frame.size.width, self.customView.forgroundView.frame.size.height);
        }];
    }
}

@end

0 个答案:

没有答案