【发布时间】:2014-03-07 18:54:35
【问题描述】:
简介
我在UIScrollView 中遇到UITableView 的问题。当我滚动外部scrollView 时,table 在第一次触摸时不会收到willSelect/didSelect 事件,但在第二次触摸时会收到。更奇怪的是,即使delegate 没有,单元格本身也会获得触摸和高亮状态。
详细说明
我的视图层次结构:
UIView
- UIScrollView (outerscroll)
- Some other views and buttons
- UITableView (tableView)
在滚动视图中,我有一些额外的视图可以动态展开/关闭。表格视图需要与视图的其他一些元素一起“固定”在顶部,这就是我创建此布局的原因,它允许我以与 Apple 推荐的类似方式轻松移动元素,当使用转换时滚动发生。
当外卷轴像这样移动时,表格视图被转换成平移效果:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.outerScrollView) {
CGFloat tableOffset = scrollView.contentOffset.y - self.fixedHeaderFrame.origin.y;
if (tableOffset > 0) {
self.tableView.contentOffset = CGPointMake(0, tableOffset);
self.tableView.transform = CGAffineTransformMakeTranslation(0, tableOffset);
}
else {
self.tableView.contentOffset = CGPointMake(0, 0);
self.tableView.transform = CGAffineTransformIdentity;
}
// Other similar transformations are done here, but not involving the table
}
在我的单元格中,如果我实现这些方法:
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
NSLog(@"selected");
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
NSLog(@"highlighted");
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touchesBegan");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSLog(@"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
NSLog(@"touchesCancelled");
}
Y 在失败时可以看到这个输出(第一次点击):
2014-02-10 13:04:40.940 MyOrderApp[5588:70b] highlighted
2014-02-10 13:04:40.940 MyOrderApp[5588:70b] touchesBegan
2014-02-10 13:04:40.978 MyOrderApp[5588:70b] touchesEnded
而这个工作时(第二次点击):
2014-02-10 13:05:30.359 MyOrderApp[5588:70b] highlighted
2014-02-10 13:05:30.360 MyOrderApp[5588:70b] touchesBegan
2014-02-10 13:05:30.487 MyOrderApp[5588:70b] touchesEnded
2014-02-10 13:05:30.498 MyOrderApp[5588:70b] expanded
在第一次和第二次点击之间没有进行其他帧更改、动画或任何其他视图交互。此外,只有在大量滚动时才会出现错误,但仅滚动几个像素时,一切都会按预期工作。
我也尝试更改一些属性,但没有成功。我做过的一些事情:
- 从滚动和表格以外的视图中删除
userInteractionEnabled - 在发生
scrollViewDidScroll时,在表格、滚动和主视图上添加对setNeedsLayout的调用。 - 从表中删除转换(仍然发生)
我看到了一些关于将UITableViews 嵌入UIScrollViews 的意外行为,但我在Apple 的官方文档中看不到这样的警告,所以我期待它能够工作。
该应用仅适用于 iOS7+。
问题
有没有人遇到过类似的问题?为什么会这样?我该如何解决?我认为我可以拦截单元格上的点击手势并使用自定义委托或类似的方式传递它,但是 我希望表格接收正确的事件,所以我的 UITableViewDelegate 收到符合预期。
更新
- 我尝试按照评论中的建议禁用单元格重用,但它仍然以相同的方式发生。
【问题讨论】:
-
你试过[yourScroll bringSubviewToFront: yourTable]; ?
-
您提到这仅在您滚动很多时才会发生,这让我认为 tableCells 的重新创建/回收可能会影响这一点?能不能在 cellForRowAtIndexPath: 函数里面放一个 NSlog,看看这个 bug 是不是在被调用的时候触发了?
-
@Ilario 谢谢,但我已经试过了。此外,单元格正在接触(失败时和工作时),并且它们在表格内,因此表格位置似乎不是问题。
-
@JackWu 这实际上是一个非常好的提示,但它也不起作用。我已经删除了单元重用来对其进行测试,并且仍然以相同的方式发生。 :(
-
请问您为什么在滚动视图中使用表格视图?这就像在滚动视图上有一个滚动视图......
标签: ios objective-c uitableview uiscrollview