Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS — Are UITableViewCell reuseIdentifiers global?

Tags:

ios

iphone

What is the scope of the table view cell's reuse identifiers — are they shared within one table view instance or within all the table views that use the same reuse identifier?

Eg, I have a FooTableViewController and a BarTableViewController, both of them have a tableView and both of them use @"Cell" identifier in tableView:cellForRowAtIndexPath, but the cell properties/styling are different. The question is - will those cells be reused across table views or not?

like image 996
Dmitry Sokurenko Avatar asked Mar 23 '12 11:03

Dmitry Sokurenko


People also ask

What is reuse identifier in IOS?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in init(frame:reuseIdentifier:) and cannot be changed thereafter.

What's the purpose of reuseIdentifier?

The reuse identifier tells the system that an object can be reused for a cell entering the screen for which you request the same identifier.

What's reuseIdentifier in cells and what's the prepareForReuse method is for?

When we call dequeueReusableCell(withIdentifier:), tableview will try to POP the cell from the queue with that identifier if it exists, if not it will return new cell. So when a cell gets reused the prepareForReuse() method will get called and you should reset the cell attributes to its normal state.

What is dequeueReusableCell?

dequeueReusableCell(withIdentifier:)Returns a reusable table-view cell object after locating it by its identifier.


1 Answers

They are never shared between instances.

A UITableView object maintains a queue (or list) of the currently reusable cells, each with its own reuse identifier, and makes them available to the delegate in the dequeueReusableCellWithIdentifier: method.

(from [UITableViewCell reuseIdentifier] docs)

Emphasis on "UITableView object".

However, you should make your cell identifiers more descriptive. A different identifier for every cell type you are using. Your code will be more readable.

like image 70
Sulthan Avatar answered Sep 29 '22 13:09

Sulthan