Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didSelectRowAtIndexPath is not getting called

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];
like image 575
vntstudy Avatar asked Oct 01 '13 09:10

vntstudy


2 Answers

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.

like image 144
NavinDev Avatar answered Oct 26 '22 11:10

NavinDev


Because scrollView overlapped on Cell... Best way is add tap Gesture on UIScrollView such like,

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
MYScrollView.userInteractionEnabled = YES;
[MYScrollView addGestureRecognizer:recognizer];

Add above code in cellForRowAtIndexPath method and Write gesture action method such like

-(void)gestureAction:(UITapGestureRecognizer *) sender
{
    CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourTableViewName];
    NSIndexPath *indexPath = [self.YourTableViewName indexPathForRowAtPoint:touchLocation];

    NSLog(@"%d", indexPath.row);
}

Here in above gesture (action) method you can get indexPath as same as didSelectRowAtIndexPath.

like image 31
iPatel Avatar answered Oct 26 '22 13:10

iPatel