didSelectRowAtIndexPath没有被调用

vnt*_*udy 9 uitableview uiscrollview didselectrowatindexpath ios

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

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

我在cellView的contentView上添加滚动视图,仍然没有调用select方法.

 [cell.contentView addSubview:scrollView];
Run Code Online (Sandbox Code Playgroud)

Nav*_*Dev 29

表格单元格无法检测到触摸的原因是因为单元格上的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];
        }
Run Code Online (Sandbox Code Playgroud)

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

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

干杯.


iPa*_*tel 13

因为scrollView重叠在Cell上...最好的方法就是添加tap Gesture UIScrollView就像这样,

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

cellForRowAtIndexPath方法和Write手势动作方法中添加上面的代码就像

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

    NSLog(@"%d", indexPath.row);
}
Run Code Online (Sandbox Code Playgroud)

在上面的手势(动作)方法中你可以得到indexPath同样的didSelectRowAtIndexPath.

  • 这种解决方案感觉"错误"和"hacky". (3认同)

Mak*_*aus 10

斯威夫特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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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