9

我在 UITableViewCell 中添加 UIScrollView,但是当我单击滚动视图时,选择方法没有被调用。

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

我在单元格的 contentView 上添加滚动视图,但它仍然没有调用 do select 方法。

 [cell.contentView addSubview:scrollView];
4

3 回答 3

29

您的表格单元格无法检测到触摸的原因是因为您的单元格上的滚动视图正在拦截触摸并且没有将其传送到表格单元格以便可以调用 tableView 委托函数。

更简单的解决方法是创建 UIScrollView 的子类并将单元格上的 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];
        }

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

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

干杯。

于 2014-05-18T18:50:46.557 回答
14

因为滚动视图在单元格上重叠......最好的方法是在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.

于 2013-10-01T09:37:01.637 回答
13

斯威夫特 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)
        }
    }
}

斯威夫特 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在情节提要或代码中为滚动视图分配类,具体取决于您创建它的方式。

于 2015-09-06T04:59:44.543 回答