-1

I have a table with 7 sections - only the first 2 sections are visible when it loads.

To simplify things - how would I print "Now at section 5" when the user scrolls and gets to that section?

I have tried all over Google but not getting the results I want. I assume the code should go in ViewDidAppear?

Yiags1978
  • 19
  • 3
  • One of the simplest ways is to check the section in `cellForRowAt` or `willDisplayCellFor`. It depends on what you are trying to do. If you went to opportunistically load data then you can look at table view prefetch protocol – Paulw11 Feb 21 '23 at 20:47
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 21 '23 at 21:14
  • Sorry, I know I should provide more code but I don't really have any in this instance. All I am trying to do is simply set a bool to true once the section has been reached. – Yiags1978 Feb 21 '23 at 21:45
  • You can simply set your `bool` to `true` when you see that section in `willDisplayCellFor` then. – Paulw11 Feb 21 '23 at 22:22
  • Thanks for your help :) I did try that in this kind of way: func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.section == 5 { setMyVar = true } } ... but this sets the variable as soon as the page is loaded as well as when I reach that section? – Yiags1978 Feb 22 '23 at 10:33

1 Answers1

0

I solved it using the code from this post in the end:

Detect UITableViewCells that appear on the screen

Thanks for your help.

Yiags1978
  • 19
  • 3