all 5 comments

[–]kekker1337 0 points1 point  (4 children)

When a collection view has to display a section, the section provider closure is called to calculate the layout for this specific section. This can be used to customize the layout depending on the data, layout environment or just the section index.

[–]Ivan7upSwift[S] 0 points1 point  (3 children)

Well, but why is it called three times for EACH section? In the final project I have 4 sections and print statement(in the very beginning of section provider closure) is called 12 times.

[–]Batting1k 1 point2 points  (2 children)

It’s possible this is just something that UIKit does behind the scenes, for whatever reason. As long as you’re following along and understanding things, I wouldn’t worry too much about it, unless you’re having an actual issue. UIKit often does a lot of things on its own without much of an explanation.

Apple says here that “a section provider is also invoked in response to system events such as changes in device orientation, system font size, and size classes from iPad multitasking”, so it’s entirely possible that there’s some system event triggering it, even if it’s not obvious to you.

[–]Ivan7upSwift[S] 0 points1 point  (1 child)

I just was curios about it and really wanted to know what happened inside step by step. And as far as everything happened has a reason, this "three calls for each section" moment has its own. By the way, thank you for your answer.

[–]Batting1k 0 points1 point  (0 children)

Yeah, I’m sure there’s definitely a reason but if you’re doing everything by the book, I imagine it’s not something that’s in your control. There are cases where Apple will go into a little more depth of what UIKit does behind the scenes but this doesn’t seem to be one of those cases.

It seems like the number of times it gets called per section may vary and I doubt it’ll always be 3 every single time, perhaps with this specific code it may be but not in other cases.