Skip to main content

What is the purpose of the reuseIdentifier?

· One min read
Ace the iOS Interview
Aryaman Sharda
Sources & Resources

Main Source: đź”— Ace the iOS Interview

Additional Sources:

Further Reading:

TL/DR

One of the performance optimizations UITableViewsandUICollectionViews make is to only initialize enough cells to fill the user’s screen. Then, whenever the user scrolls, instead of instantiating a new cell, it can just replace the contents of an existing previously allocated cell the cell that is about to be scrolled off the screen. This approach is not only very performant, but also utilizes less memory.

Imagine a UITableView with multiple custom UITableViewCells.In order to perform the optimization mentioned above, the UITableView needsto be able to quickly find a cell that differs only in content, but shares the same layout.

This is exactly the problem that reuse identifiers solve.

UITableViews use reuse identifiers to understand what rows (if any) differ only in content and not in layout. These cells then become candidates for reuse.

That’s why if you have a UITableView with multiple UITableViewCell types, you’ll need to register multiple cell reuse identifiers.

tableView.registerClass(MyCustomCell.self, forCellReuseIdentifier: "MyCustomCell")