Ios 未调用DidSelectRowatineXpath

Ios 未调用DidSelectRowatineXpath,ios,uitableview,uiscrollview,didselectrowatindexpath,Ios,Uitableview,Uiscrollview,Didselectrowatindexpath,我正在UITableViewCell中添加UIScrollView,但当我单击scroll view时,并没有调用select方法 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 我在单元格的contentView上添加了滚动视图,仍然没有调用did select方法 [cell.contentView addSubview:scrollView]; 因为滚

我正在UITableViewCell中添加UIScrollView,但当我单击scroll view时,并没有调用select方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
我在单元格的contentView上添加了滚动视图,仍然没有调用did select方法

 [cell.contentView addSubview:scrollView];

因为滚动视图在单元格上重叠。。。最好的方法是在
UIScrollView
上添加点击手势,例如

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
MYScrollView.userInteractionEnabled = YES;
[MYScrollView addGestureRecognizer:recognizer];
cellforrowatinexpath
方法中添加上述代码,并编写手势动作方法,如

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

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

在上面的手势(动作)中方法您可以获取
indexPath
,与
didSelectRowAtIndexPath
相同。表格单元格无法检测触摸的原因是单元格上的滚动视图正在拦截触摸,并且没有将其传输到表格单元格,以便调用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];
        }
它基本上检查滚动视图是否被拖动,如果没有,它会将所有触摸事件传递给它的superview,在本例中是cell content视图

这为我解决了一个类似的问题,希望这有帮助

干杯

斯威夫特2
class UIScrollViewSuperTaps:UIScrollView{
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果自动拖动{
super.touchsbegind(touchs,withEvent:event)
}否则{
self.superview?触摸开始(触摸,withEvent:event)
}
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
如果自动拖动{
super.touchscancelled(touchs,withEvent:event)
}否则{
self.superview?.touchscancelled(触摸,带事件:事件)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果自动拖动{
super.touchesend(touchs,withEvent:event)
}否则{
self.superview?.touchesend(触摸,带事件:事件)
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果自动拖动{
super.touchsmoved(touchs,with event:event)
}否则{
self.superview?触摸已移动(触摸,带事件:事件)
}
}
}
斯威夫特3
class UIScrollViewSuperTaps:UIScrollView{
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果自我欺骗{
super.touchesbeated(touches,with:event)
}否则{
self.superview?.touchsbegind(触摸,带有:事件)
}
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
如果自我欺骗{
super.touchscancelled(触摸,带有:事件)
}否则{
self.superview?.touchscancelled(触摸,带有:事件)
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果自我欺骗{
super.touchesend(触摸,带有:事件)
}否则{
self.superview?.touchesend(触摸,带有:事件)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果自我欺骗{
super.touchesMoved(touches,with:event)
}否则{
self.superview?.touchesMoved(触摸,带有:事件)
}
}
}

别忘了根据您创建滚动视图的方式,将class
UIScrollViewSuperTaps
指定给storyboard中的滚动视图或代码中的滚动视图。

Becz scrollview将执行触摸操作,而不是单元格为什么要在tablview上添加滚动视图?创建tablview时,它会自动添加滚动视图。scrollview是TableView的子脚本。我在table view单元格中添加了水平滚动视图。这是一个比上面发布的手势识别器更好的解决方案。这是一个更好的解决方案,应该是答案。:)太神了我已经想到了类似的方法,但你只是提供了一个简单的解决方案,绝对是聪明而简单的:)@NavinDev如果我创建一个示例项目,这个方法就行了。但在我的应用程序中不起作用,因为我有TabbarController及其委托集。有什么原因使它不起作用吗?@NavinDev我发现,如果在代码中的某个地方有其他功能的UITapgestureRecograiser,这将不起作用。但问题的主要目的已经实现了!!这种解决方案感觉是错误的,但它在scrollview中只有一个tableview的情况下工作。如果我们有多个tableview/collectionview怎么办?很多电子商务应用程序在scrollview中都有多个CollectionView。在那种情况下,这个解决方案不起作用。这个效果很好。但是,请确保您没有在viewcontroller中任何其他具有UIScrollViewSuperTaps scrollview的位置使用AppgestureRecognitors。它不起作用。事件未传递到tableview。我无法使用CellView。我的tableview委托未调用。在2020年也可以使用;)
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)
        }
    }
}
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)
        }
    }
}