didSelectRowAtIndexPath is not getting called

vntstudy picture vntstudy · Oct 1, 2013 · Viewed 7.2k times · Source

I am adding UIScrollView in UITableViewCell, but when I am click on scroll view did select method is not getting called.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

I am adding the scroll view on contentView of cell, still its not calling did select method.

 [cell.contentView addSubview:scrollView];

Answer

NavinDev picture NavinDev · May 18, 2014

The reason your table cell is not being able to detect the touches is because the scrollView on your cell is intercepting the touches and is not conveying it to the table cell so that the tableView delegate function can be called.

A simpler fix is just create a subclass of UIScrollView and set the scrollView on your cell to this subclass. override these methods on the scrollview subclass

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        if (!self.dragging)
            [self.superview touchesCancelled: touches withEvent:event];
        else
            [super touchesCancelled: touches withEvent: event];
    }

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
            if (!self.dragging)
                [self.superview touchesMoved: touches withEvent:event];
            else
                [super touchesMoved: touches withEvent: event];
        }

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
            if (!self.dragging)
                [self.superview touchesBegan: touches withEvent:event];
            else
                [super touchesBegan: touches withEvent: event];
        }

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
            if (!self.dragging)
                [self.superview touchesEnded: touches withEvent:event];
            else
                [super touchesEnded: touches withEvent: event];
        }

Which basically checks whether the scroll view is being dragged , and if its not it passes all the touch events to its superview , in this case the cell content view.

This fixed a similar problem for me , hope this helps.

Cheers.