didSelectRowAtIndexPath没有被调用

时间:2013-10-01 09:29:00

标签: ios uitableview uiscrollview didselectrowatindexpath

我在UITableViewCell中添加了UIScrollView,但是当我点击滚动视图时,确实没有调用select方法。

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

我在单元格的contentView上添加了滚动视图,但仍未调用select方法。

 [cell.contentView addSubview:scrollView];

3 个答案:

答案 0 :(得分:28)

您的表格单元格无法检测到触摸的原因是因为单元格上的scrollView正在拦截触摸并且没有将其传递给表格单元格,因此可以调用tableView委托函数。

更简单的修复方法是创建UIScrollView的子类,并将单元格上的scrollView设置为此子类。 在scrollview子类

上覆盖这些方法
-(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];
        }

它基本上检查滚动视图是否被拖动,如果不是它将所有触摸事件传递给它的超级视图,在这种情况下是单元格内容视图。

这为我解决了类似的问题,希望这会有所帮助。

干杯。

答案 1 :(得分:12)

因为scrollView在Cell上重叠...最好的方法是在UIScrollView添加点击手势,例如,

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

cellForRowAtIndexPath方法和写手势操作方法中添加上述代码,例如

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

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

在上面的手势(动作)方法中,您可以indexPathdidSelectRowAtIndexPath相同。

答案 2 :(得分:9)

Swift 2

class UIScrollViewSuperTaps: UIScrollView {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if self.dragging {
            super.touchesBegan(touches, withEvent: event)
        } else {
            self.superview?.touchesBegan(touches, withEvent: event)
        }
    }

    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        if self.dragging {
            super.touchesCancelled(touches, withEvent: event)
        } else {
            self.superview?.touchesCancelled(touches, withEvent: event)
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if self.dragging {
            super.touchesEnded(touches, withEvent: event)
        } else {
            self.superview?.touchesEnded(touches, withEvent: event)
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if self.dragging {
            super.touchesMoved(touches, withEvent: event)
        } else {
            self.superview?.touchesMoved(touches, withEvent: event)
        }
    }
}

Swift 3

class UIScrollViewSuperTaps: UIScrollView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if self.isDragging {
            super.touchesBegan(touches, with: event)
        } else {
            self.superview?.touchesBegan(touches, with: event)
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)    {
        if self.isDragging {
            super.touchesCancelled(touches, with: event)
        } else {
            self.superview?.touchesCancelled(touches, with: event)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if self.isDragging {
            super.touchesEnded(touches, with: event)
        } else {
            self.superview?.touchesEnded(touches, with: event)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if self.isDragging {
            super.touchesMoved(touches, with: event)
        } else {
            self.superview?.touchesMoved(touches, with: event)
        }
    }
}

不要忘记将课程UIScrollViewSuperTaps分配到故事板或代码中的滚动视图,具体取决于您创建它的方式。