【问题标题】:didSelectRowAtIndexPath is not getting calleddidSelectRowAtIndexPath 没有被调用
【发布时间】:2013-10-07 09:39:09
【问题描述】:

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

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

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

 [cell.contentView addSubview:scrollView];

【问题讨论】:

  • Becz 滚动视图将采取触摸操作而不是单元格
  • 为什么要在 tablview 上添加滚动视图,它会在您创建 tablview 时自动添加。 scrollview 是 tablview 的子类..
  • 我在表格视图单元格中添加水平滚动视图

标签: ios uitableview uiscrollview didselectrowatindexpath


【解决方案1】:

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

【讨论】:

  • 这很好用。但请确保您没有在视图控制器中拥有 UIScrollViewSuperTaps 滚动视图的其他任何地方使用 uitapgesturerecognizers。
  • 它不工作。事件未传递到 tableview 单元格
  • 对我不起作用。我的 tableview 委托没有被调用。
  • 在 2020 年也能闪亮登场 ;)
【解决方案2】:

您的表格单元格无法检测到触摸的原因是因为您的单元格上的滚动视图正在拦截触摸并且没有将其传送到表格单元格以便可以调用 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];
        }

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

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

干杯。

【讨论】:

  • 这是一个比上面发布的手势识别器更好的解决方案。
  • 这是一个更好的解决方案,应该是答案。 :)
  • 太棒了!我已经想到了这样的事情,但你只是提供了一个简单的解决方案,绝对精彩而简单:)
  • @NavinDev 如果我创建一个示例项目,这将有效。但不适用于我的应用程序,其中我有带有委托集的 tabbarcontrollers。有什么理由不工作吗?
  • @NavinDev 我发现如果您的代码中某处有 uitapgesturerecogniser 用于其他功能,这将不起作用。但是问题的主要目的达到了!!
【解决方案3】:

因为 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);
}

在上面的手势(action)方法中你可以得到indexPathdidSelectRowAtIndexPath一样。

【讨论】:

  • 这个解决方案感觉不对,但如果你在滚动视图中只有一个表格视图,它可以工作。如果我们有多个 tableview / collectionview 怎么办?许多电子商务应用程序在滚动视图中有多个集合视图。在这种情况下,此解决方案将不起作用。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多