UITableView scroll performance paradox: Make subviews or use -drawRect :?

I read this example (link) ...

From the description, this seems to me a paradox:

When implementing a table view cell, there is tension between optimal scroll performance and optimal editing / reordering. You should usually use subviews in viewing the contents of a cell.

When you edit or reorder controls using subviews, it makes implementations and animations work better because UIKit does not need to be redrawn during the animation.

Conclusion: Subviews are faster because UIKit should not redraw during animation. Therefore, when scrolling, it should not call -drawRect: all the time. Right?

And then the paradox:

If the content is complex, however (more than three subzones), scroll performance may suffer. If this becomes a problem, you can instead draw directly in the sub-item a presentation of the contents of the table cell.

Conclusion: Prefer -drawRect: draw directly in the table view cell. Better scroll performance for complex cells. Better than a lot of sub-sectors.

So good. What is the truth? Subviews or -drawRect: which is better?

In my case, I would have 12 subzones per cell. Are they just trying to say that? "If you have only up to 3 subheadings, go with them, but if you have much more, use -drawRect: and draw directly on the cell." But then: why don't they always prefer -drawRect: then?

+4
source share
1 answer

You can summarize as Display only Vs Functional. If your cells are tasked with simply displaying data that changes little and remains static, draw it. If your cells are tasked with being functional, you can change the position or manipulate the user (or you have a custom animation), see how to use subviews.

There really is no hard and fast rule. Your use case is a good measurement that fits best. With 12 subviews, I would instantly lean towards custom drawing for speed benefits.

Why don't they always prefer -drawRect: then?

You get quite a few things using subviews. Animations are easier. For example, using UIImageView to display an image gives you backlighting for free. It really depends on what your cell is associated with.

+6
source

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


All Articles