-2

I have checked all possibilities as to why my UITableView is behaving so oddly for the whole day. Finally, right now I noticed something strange. I needed some help in figuring out if what I noticed is true or the problem could be in someplace else.

So in tableView(_:didSelectRowAt:), I want to scroll to a particular cell after resizing the heights of all the cells. (I am increasing the heights of all the cells.)

This is a small relevant part of my scrollViewDidScroll(_) method body that is responsible for deciding which part of the section of screen was tapped (I need this for some other stuff to work):

public func scrollViewDidScroll(_ scrollView: UIScrollView) {        
   let touchLocation = scrollView.panGestureRecognizer.location(in: containerView)
   let touchX = touchLocation.x
   print("X: \(touchX)")
  .
  .
  .
}

Now in my didSelectRowAt, when I call scrollToRow as:

tableView.scrollToRow(at: indexPath, at: .top, animated: false)

I get this as output:

X: 132.0 (old touch x)
X: 130.66665649414062 (old touch x when scrolling stopped)
X: 188.66665649414062 (new touch x of tap)
X: 188.66665649414062 (not sure why this was printed again)

But now when I execute the scrollToRow as:

tableView.scrollToRow(at: indexPath, at: .top, animated: true)

X: 160.3333282470703
X: 160.3333282470703 (new touch x of tap)
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
X: 177.66665649414062
.
.
.
174 times .
.
.
X: 177.66665649414062

This is very strange. Can someone explain why this could be happening?

You can get the code at https://github.com/parthv21/DeepScroll

Parth
  • 2,682
  • 1
  • 20
  • 39

2 Answers2

0

Sorry for before answer. maybe this will help, scrollViewDidScroll every time when you scroll tableview so, may be you have to use scrollViewDidEndDragging,scrollViewWillBeginDecelerating,scrollViewDidEndDecelerating

viral goti
  • 63
  • 10
0

scrollViewDidScroll gets called every frame that the scroll view has scrolled, doesn’t matter if it’s done by the user or by calling scrollTo methods. This can be up to, I think, 120 times per second while the user is scrolling.

If you want to do something when the scroll view has stopped, you should use the didEndDeccelerating and didEndScrollingAnimation delegate methods.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • But then why does it remember the X location of touch each time? Is there any way to consume the touch event so that the next time value of X is 0? – Parth Nov 15 '19 at 19:35
  • I would guess that it resets only when the dragging starts. What are you trying to do? – EmilioPelaez Nov 15 '19 at 23:05
  • I solved the issue. Thanks for the answer. I had to add a flag to prevent the code inside didScroll from being called again and again. Then after scroll ended, I turned the flag off. – Parth Nov 16 '19 at 14:52