Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton inside UITableViewCell steals touch from UITableView

I have a problem similar to this one but answer provided there doesn't help much. I have UITableView with some custom UITableViewCells, those cells have some nested custom UIViews and finally some UIButtons inside. The problem, as in question specified above, is that when I touch my button touch event won't populate to UITableView and it never scrolls.

Here's some code (it's just fastest way to reproduce, it's not my actual code):

@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

Red button in the only cell won't let table view to bounce scroll.

Can anyone help me a bit?

P.S. Don't pay attention to the stupidity of the code I've provided, I'm aware of all issues about it. I just provided it to show what the issue is all about. It's the conflict of buttonView and scrollView.

like image 561
DemoniacDeath Avatar asked Apr 12 '13 07:04

DemoniacDeath


1 Answers

This is the standard behaviour of UIScrollView which tableviews use. The system doesn't know that you want to scroll until you move your finger, but by that time you have already "pressed" on the button and so it assumes that's what you want to do.

You can play with a couple of properties on your tableview's scrollview to change the behaviour, but you may find they negatively impact the feel of your cells and buttons because of added delays.

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.

like image 126
Mike Weller Avatar answered Oct 21 '22 06:10

Mike Weller