SwipeableTableViewCell

SwipeableTableViewCell

我希望创建通用的可滑动SwipeableTableViewCell。因此,我有一些xib,并带有扩展了SwipeableTableViewCell的swift文件。在SwipeableTableViewCell中,我以编程方式添加UIScrollView来查看conetntView并将其移至该scrollView。结果,我可以使用界面生成器并添加ex。 UIButtonsscrollView而不是使用UITableViewRowAction

这不是我的全部想法,它基于此code

这就是完成的方式。

SwipeableTableViewCell.swift

super.awakeFromNib()之后调用的方法

private func setup() {
    // Create the scroll view which enables the horizontal swiping.
    let scrollView = UIScrollView(frame: self.contentView.bounds)
    scrollView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]

    scrollView.delegate = self
    scrollView.scrollsToTop = false
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false
    self.scrollView = scrollView

    self.addSubview(scrollView)

    var contentBouds = self.contentView.bounds.size
    contentBouds.height = 0
    self.scrollView.contentSize = contentBouds
    self.backgroundColor = self.contentView.backgroundColor

    // Create the containers which will contain buttons on the left and right sides.
    self.buttonViews = [self.createButtonsView(),self.createButtonsView()]

    // Set up main content area.
    let newContentView = UIScrollView(frame:scrollView.bounds)
    newContentView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
    newContentView.backgroundColor = UIColor.whiteColor()
    scrollView.addSubview(newContentView)
    self.scrollViewContentView = newContentView

    self.contentView.removeFromSuperview()
    self.scrollViewContentView.addSubview(self.contentView)
}


问题

我的问题是,在将contentView移至scrollViewContentView之后,它会在点击时停止反应。我猜这是因为conetntView已从视图层次结构中删除。在becomeFirstRespondercontentViewscrollViewContentView中调用scrollView会造成麻烦。

我怎样才能做到这一点?可能吗

更新

这个问题的工作原理令人惊讶。我可以在问题上挣扎几天,一旦我发布问题,就会发现新的痕迹。

最佳答案

您好,我下载了您的repo并通过添加自定义滚动视图并作为委托传递给您的自定义单元格来修复代码,这是代码

- (void)setUp {
// Create the scroll view which enables the horizontal swiping.
SwipeableScrollView *scrollView = [[SwipeableScrollView alloc] initWithFrame:self.contentView.bounds];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentSize = self.contentView.bounds.size;
scrollView.delegate = self;
scrollView.scrollsToTop = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[self.contentView addSubview:scrollView];
self.scrollView = scrollView;
self.scrollView.customDelegate = self;

//self.scrollView.userInteractionEnabled = NO;

// Create the containers which will contain buttons on the left and right sides.
self.buttonViews = @[[self createButtonsView], [self createButtonsView]];

// Set up main content area.
UIView *contentView = [[UIView alloc] initWithFrame:scrollView.bounds];
contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
contentView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:contentView];
self.scrollViewContentView = contentView;

// Put a label in the scroll view content area.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(contentView.bounds, 10, 0)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.scrollViewContentView addSubview:label];
self.scrollViewLabel = label;

// Listen for events that tell cells to hide their buttons.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleCloseEvent:)
                                             name:kSwipeableTableViewCellCloseEvent
                                           object:nil];


}

我也加上这个

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[self class] closeAllCellsExcept:nil];
    [super touchesBegan:touches withEvent:event];
}


这是SwipeableScrollView的.h

#import <UIKit/UIKit.h>

@interface SwipeableScrollView : UIScrollView

@property (weak,nonatomic) UIResponder * customDelegate;

@end


这是.m

#import "SwipeableScrollView.h"

@implementation SwipeableScrollView


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.customDelegate touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.customDelegate touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.customDelegate touchesEnded:touches withEvent:event];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


希望对您有所帮助,我也对您的回购请求有要求!

09-12 01:34