RONY ROZEN: If your app displays large amounts of data,
chances are you're using-- or at least should be using--
a UITableView or a UICollectionView.
The goal of these UI components is
to make your life easier when dealing
with large amounts of data.
But if you don't use them properly,
you can end up with a far from perfect user experience.
Let me share a couple of UITableView optimization tips
that are also relevant for UICollectionView.
Your users will thank you for this.
[MUSIC PLAYING]
If you don't optimize your UITableView use,
there will be a lag when the user is scrolling.
That's, obviously, not the smooth user
experience you want to provide.
So what can you do?
Let's start with the simplest, yet most significant tip.
We use Identifier.
You see, the way you UITableView works
is that new cells are rendered right
before they enter the screen as the user is scrolling.
At the same time, other cells that
have recently left the screen are waiting to be reused.
So using these cells that were already rendered
will be significantly more efficient than creating
new cells from scratch.
The way to do this is to provide each type of cell
with a reuse identifier when it's initially created,
and ask for cells with the same identifier
whenever a new cell needs to be rendered.
This is done using UITableView's dequeueReusableCell method.
You provide the relevant use identifier string,
and the method returns a cell with the same user identifier.
If there is an already rendered cell that can be reused,
this is what the method will return.
If you have a custom class for this cell,
you should use the register cellClass
forCellReuseIdentifier method, so
that the dequeueReuseableCell method will not fetch or create
an instance of the matching class.
Note that if you have multiple cell types,
say, a list of classes and a list of students
for each class, you should make sure to use different reuse
identifiers for each type of cell.
This will ensure that when calling
the dequeueReuseableCell method, the return cell
will have the right structure, and will save you
the setup time.
So now that we have our new UITableView cell object,
or a custom subclass, we need to make
sure it displays the relevant content.
This is done in the TableView cellForRow indexPath method.
Our goal is to make this method implementation as short
and to the point as possible.
This is not the place for complex computations,
image manipulations, or any other time-consuming actions.
All of the heavy lifting needs to be done elsewhere
and be prepared by the time this method is reached.
Otherwise, you guessed it, you can
forget about smooth scrolling.
Needless to say that it is not the place for dealing
with off-screen displays.
Anything that isn't going to be presented in the cell,
or can be calculated elsewhere, and not
in the cellForRow indexPath method doesn't belong here.
In addition to keeping this method implementation
short, quick, and to the point, you
need to make sure that you're loading images
from the web on a background thread.
You don't want your display to lag, or even worse, get stuck,
while you're waiting for an image
to be loaded and displayed.
It'll be a significantly better user experience
to show the cell immediately and have the content from the web
join a short while after, when it's ready.
Make sure to also cache everything
you can so that the next time the user
scrolls to a row that was already displayed in the past,
there will be no need to wait.
The image will appear immediately,
since it was already loaded and cached.
There are multiple open source libraries out there
that can save you the effort of implementing
these improvements yourself.
A simple Google Search can find several of them,
and you can pick your personal favorite.
One example is Kingfisher, a lightweight, swift library
for downloading and caching images from the web.
The simplest use case will look like this.
This will allow you to load the image of your choosing,
cache it, and if you attempt to load the same image later,
it will be retrieved from cache and not reloaded from the web.
Another tip that can significantly
improve the scrolling experience is
to make sure that the cell's background is completely
opaque, and, if possible, has a single consistent background
color, and not a gradient, for example.
So unless you need the cell's background to be transparent,
and are willing to possibly pay for it in performance,
make sure to have an opaque background color for each cell,
especially if it's the same color as the view's background.
I used UITableView for these tips,
but they are also relevant UICollectionView.
Give them a try and see for yourself how much smoother
you're scrolling gets.
I can assure you your users will notice and appreciate it
as well.
Until next time, I'm Rony Rozen for "Route 85."
Thanks for watching.
[MUSIC PLAYING]