UITableView does not work the first time after changing restrictions

IMPORTANT: My problem is not what I implement instead . Already verified that :) didDeelectRowAt didSelectRowAt

I have UITableViewone that is displayed on a part of the screen in a modally presented view controller. When the user drags it, it changes to full screen and returns to a certain minimum height. I do this by implementing the following methods from 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)
}

Everything regarding resizing and scrolling works fine, but there is a problem that I cannot understand why this is happening. This is the following:

  • When a view controller with a table view is displayed for the first time with a minimum height, and I click on a cell, it works fine.
  • , , .
  • , , , , , , UIScrollViewDelegate UITableViewDelegate , , .

, , , . , didSelectRowAt.

:

: https://github.com/nikmin/DragTest

, , , -, - , , .

... , , , < 0 , .

+4
3

, ( UX) .

, , , , . , . , , ( ), . .

, , . , , . , . , - , ( ).

+1

TableView, , , , :

  • , , scrollView.isDecelerating - true. , scrollViewDidScroll .

  • TableView, , scrollViewDidScroll - false. , scrollViewDidScroll . .

guard !scrollView.isDecelerating else { return } scrollViewDidScroll. TableView .

, .

, ;)

+3

It appears that the content offset is not configured correctly. The first touch cancels the wrong position (unscroll), so it is not registered. Perhaps it would be better if we got access to the full code to check it, because I can’t tell you exactly where the problem lies, but I think this is in the method scrollViewDidScroll.

0
source

Source: https://habr.com/ru/post/1688635/


All Articles