自定义 UITableViewCell 不响应触摸

Custom UITableViewCell don't react on touch

我希望创建通用的可滑动 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 已从视图层次结构中删除。在 contentViewscrollViewContentViewscrollView 中调用 becomeFirstResponder 无济于事。

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

更新

这个问题的工作原理真是太神奇了。我可以为问题苦苦挣扎几天,一旦我 post 提出问题,就会出现新的痕迹。

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

- (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

希望对您有所帮助,我也有一个对您的回购的拉取请求!