10

我有一个与此类似的问题,但提供的答案并没有多大帮助。我有UITableView一些自定义UITableViewCells,这些单元格有一些嵌套的自定义UIViews,最后是一些 UIButtons。如上所述,问题是当我触摸我的按钮时,触摸事件不会填充UITableView并且它永远不会滚动。

这是一些代码(这只是最快的重现方式,不是我的实际代码):

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView * tableView;

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
                                                                       self.view.bounds.size.width,
                                                                       self.view.bounds.size.height)
                                                      style:UITableViewStylePlain];
        [self.view addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(0, 0, 50, 44);
    [cell.contentView addSubview:button];
    return cell;
}

@end

唯一单元格中的红色按钮不会让表格视图反弹滚动。

谁能帮我一点忙?

PS不要注意我提供的代码的愚蠢性,我知道有关它的所有问题。我只是提供它来显示问题的全部内容。buttonView这是和的冲突scrollView

4

4 回答 4

11

You can change the behaviour by overriding touchesShouldCancelInContentView: in the UITableView. For this to work you'll need to replace the table view with this subclass, either in loadView or in your xib file.

@interface AutoCancelTableView: UITableView
@end

@implementation AutoCancelTableView

//
// Overriding touchesShouldCanceInContentView changes the behaviour
// of button selection in a table view to act like cell selection -
// dragging while clicking a button will cancel the click and allow
// the scrolling to occur
//
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return YES;
}

@end
于 2013-10-27T01:13:15.000 回答
9

这是UIScrollViewtableviews 使用的标准行为。在您移动手指之前,系统不知道您要滚动,但到那时您已经“按下”按钮,因此它假定这就是您想要做的。

您可以在 tableview 的滚动视图上使用几个属性来更改行为,但您可能会发现它们会对单元格和按钮的感觉产生负面影响,因为会增加延迟。

self.tableView.delaysContentTouches = YES;

delaysContentTouches If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent...

and

self.tableView.canCancelContentTouches = YES

canCancelContentTouches If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent: message and the scroll view handles the touch as a scroll.

于 2013-04-12T08:14:06.087 回答
1

Because all the above answers not working for me. I add a imageView on the button and make the imageView user interface YES, then add a tap gesture on the iamgeView. In the tap gesture related methods I put the button related methods in. so all is well.. may be hack. But it work well..

于 2014-03-18T04:41:01.037 回答
0

下面的代码对我有用:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
cell.userInteractionEnabled = YES;
if (cell == nil) {
    cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"]autorelease];
}

[cell.button addTarget:self action:@selector(button_action) forControlEvents:UIControlEventTouchUpInside];}

-(void)button_action{NSLog(@"Hello!");}

这是我的自定义单元格:

#import "CustomCell.h"
@implementation CustomCell
@synthesize button;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    button = [[UIButton alloc]init];
    button =[UIColor redColor];
    [self.contentView addSubview: button];
           }
return self;}

- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+50 ,+15, 100, 100);
        button = frame;}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{   [super setSelected:selected animated:animated];
// Configure the view for the selected state
}
于 2013-04-12T08:01:05.983 回答