0
votes

I am trying to log some events when a user scrolls the 50% of the scrollView contents.

I can get the scroll amount with scrollView.contentOffset.y as following:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f",scrollView.contentOffset.y);
}

Swift:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print(scrollView.contentOffset.y)
}

As I scroll down to the bottom of the scrollView I can find out about the height of contentArea once I hit the bottom, lets say that value is 2000 and I can hardcode the value of 1000 since I am interested only when user exceeds the 50% of the whole contentSize.

But I want it to be dynamic, since dataSource of tableView might change in the future, which would result in the bottom of scrollView's offset to be greater or less than 2000.

I would like to know if it is possible to detect the maximum contentOffset value for a tableView even before scrolling to the bottom?

Thanks in advance.

1
If you are using a non-sectioned tableView, collectionView maybe is better to check willDisplayCell or cellForRow and check if the indexPath "is" on the middle of your objects and if the cells are of same height. If not maybe and you use UITableViewAutomatic Dimension then I think is not possible to know it exactly in advance. If you know the height of cells you can by making a calculation.Andreas777
Based on your question, it sounds like you have dynamic sized rows / cells, with a dynamic dataSource, yes? If so, think about it logically... You are asking "How can I get the total height of all the variable-sized rows, without rendering all the rows?" Simple answer: you can't, really. You could write a function to loop through the entire data set and manually calculate the heights, but that may or may not work for your data.DonMag
Yeah I will try to have a workaround, I will comment here if I can implement one, thanks guys.emrepun
There is a property named "contentSize" of the UIScrollView. developer.apple.com/documentation/uikit/uiscrollview/… You may use this after you populate your table view to get the total height your table view can take. Recommend you to test it once.kerry
Yeah thats what I needed @kerry thank you so much!emrepun

1 Answers

1
votes

There is a property named "contentSize" of the UIScrollView.

https://developer.apple.com/documentation/uikit/uiscrollview/1619399-contentsize

You may use this after you populate your table view to get the total height your table view can take.

NOTE: This is valid only if your rows have constant height rather than a variable height