自定义UITableViewCell在触摸时不会做出反应

时间:2016-05-30 18:52:43

标签: ios uitableview uiscrollview custom-cell

我希望创建通用可刷卡SwipeableTableViewCell。所以我有一些xib,以及扩展SwipeableTableViewCell的swift文件。在SwipeableTableViewCell我正在以编程方式添加UIScrollView来查看并将conetntView移至此scrollView。在结果中,我可以使用界面构建器并添加ex。 UIButtons改为scrollView而不是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而感到茫然无助。

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

更新

令人惊讶的是这个问题是如何运作的。我可以在几天内解决问题,一旦发布问题,就会出现新的痕迹。

1 个答案:

答案 0 :(得分:1)

您好我通过添加自定义滚动视图下载您的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

希望这个帮助,我也有一个请求你的回购!