2013-04-12 48 views
9

抢断触摸我有类似this one问题,但答案只要没有太大的帮助。 我UITableView一些定制UITableViewCells,这些细胞有一定的嵌套自定义UIViews最后一些UIButtons内。正如上面指出的那样,问题在于当我触摸我的按钮时,触摸事件不会填充到UITableView,并且它从不滚动。的UIButton内的UITableViewCell从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 

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

谁能帮我一点吗?

P.S.不要关注我提供的代码的愚蠢,我意识到所有关于它的问题。我只是提供它来显示问题的全部内容。这是buttonViewscrollView的冲突。

回答

9

这是UIScrollView其中tableviews使用标准的行为。系统不知道要滚动,直到你移动你的手指,但那时的按钮,你已经“压”,所以它假定这就是你想要做什么。

您可以与您的tableview的滚动视图去改变几个属性的发挥,但你会发现,他们的感觉产生负面影响您的细胞,因为加入延迟的按钮。

self.tableView.delaysContentTouches = YES; 

delaysContentTouches 如果此属性的值是YES,滚动视图延迟处理该下接触手势,直到它可以决定是否滚动是意图...

self.tableView.canCancelContentTouches = YES 

个canCancelContentTouches 如果该属性的值是YES,并在内容的图已经开始跟踪手指触摸它,如果用户拖动手指够以发起滚动,视图接收touchesCancelled:withEvent:方法消息和滚动视图将触摸作为滚动来处理。

+8

我仍然无法得到这个'UITontrol'在'UITableView'里面工作。此外,因为'UITableView'是'UIScrollView'的子类,所以你的代码应该是'self.tableView.canCancelContentTouches = YES;' – johnboiles

+1

@johnboiles你应该设置elf.tableView.scrollView.delaysContentTouches = NO; – passol

+0

答案中显示的组合不起作用。 @johnboiles是正确的。刚刚为我工作的是:mytable.delaysContentTouches = false和mytable.canCancelContentTouches = true。这使得我的细胞内的按钮就像平常一样工作。 – zumzum

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 
} 
+0

那么,它不适合我 - 触摸按钮仍然可以防止滚动tableView。不管怎么说,还是要谢谢你。 – DemoniacDeath

7

您可以通过在UITableView中重写touchesShouldCancelInContentView来更改行为:为了这个工作,你需要用loadView或者xib文件替换这个子类的表视图。

@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 
+0

感谢yooooouuuuu!现在表格视图页脚中的单元格和按钮的行为完全相同......;) – Trenskow

+0

完美的答案,像魅力一样工作。 –

+0

可以确认此作品。只要确保将它与'tableView.delaysContentTouches = false'耦合即可。 – mxcl

1

因为以上所有答案都不适用于我。我在按钮上添加一个imageView,并使imageView用户界面为YES,然后在iamgeView上添加一个轻击手势。在轻拍手势相关的方法中,我将按钮相关的方法放在中。所以一切都很好.. 可能是破解。但它工作得很好..

相关问题