重要:我的问题不是我正在实现 didDeelectRowAt
而不是 didSelectRowAt
强>。已经检查过了:)
我有一个 UITableView
,它显示在模态呈现的 View Controller 的部分屏幕上。当用户拖动时,它会调整为全屏大小并返回到某个定义的最小高度。我通过从 UIScrollViewDelegate
实现以下方法来做到这一点:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !scrollView.isDecelerating else { return }
let contentOffset = scrollView.contentOffset.y
if tableViewHeightConstraint.constant < view.frame.height && contentOffset > 0.0 {
tableViewHeightConstraint.constant = min(contentOffset + tableViewHeightConstraint.constant, view.frame.height)
scrollView.contentOffset.y = 0.0
return
}
if tableViewHeightConstraint.constant > minViewHeight && contentOffset < 0.0 {
tableViewHeightConstraint.constant = max(tableViewHeightConstraint.constant + contentOffset, minViewHeight)
scrollView.contentOffset.y = 0.0
}
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Here I have some calculations if depending the dragging end position and the velocity the end size should be full screen or `minViewHeight`
// After calculating what the end size should be I'm animating the size change
heightConstraint.constant = newConstraintHeight
UIView.animate(withDuration: TimeInterval(calculatedAnimationDuration), delay: 0.0, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
关于调整大小和滚动的一切工作正常,但有一个问题,我无法弄清楚为什么会这样。内容如下:
- 当带有表格 View 的 View Controller 第一次以最小高度显示时,我点击一个单元格,它工作正常。
- 如果我拖动以将表格 View 扩展到全屏高度并点击一个单元格,它再次正常工作。
- 如果我拖动以将表格 View 扩展到全屏高度,然后再次拖动以将其返回到最小高度,然后点击一个单元格,则没有任何事情发生,没有
UIScrollViewDelegate
或UITableViewDelegate
方法被调用。如果我再次点击一个单元格,一切正常。
我注意到的一件事是,在将表格 View 拖回最小高度后,滚动指示器不会隐藏。在第一次点击时它隐藏,在第二次点击时调用 didSelectRowAt
。
更新:
这是问题的测试 repo :https://github.com/nikmin/DragTest
如果拖动不完美,请不要介意,我只是放了一些东西让任何人都可以尝试,但我认为这个问题很容易重现。
还有一件事...如果您从全尺寸一直拖动到底部,使表格 View 达到最小高度,然后继续拖动,使内容偏移量 < 0 并释放,则不会发生问题.
最佳答案
拖动 TableView
使其恢复到最小高度,然后点击一个单元格,没有任何反应,因为:
当您拖动以将 TableView 扩展到全屏时,
scrollView.isDecelerating
为true
。因此scrollViewDidScroll
方法中的代码将运行。但是当您拖动
TableView
使其返回到最小高度时,scrollViewDidScroll
为false
。所以scrollViewDidScroll
方法中的代码不会运行。它让第一次点击什么都不做。
只需从 scrollViewDidScroll
中删除 guard !scrollView.isDecelerating else { return }
。将 TableView
向下拖动后,您将正常点击单元格。
但是你需要稍微改变一下逻辑,去掉上面那行动画会出错。
希望对你有帮助;)
关于ios - 约束更改后 UITableView 水龙头第一次不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083254/